1 ! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types arrays continuations io
4 io.backend.windows io.pipes.windows.nt io.pathnames libc
5 io.ports windows.types math windows.kernel32 namespaces make
6 io.launcher kernel sequences windows.errors splitting system
7 threads init strings combinators io.backend accessors
8 concurrency.flags io.files assocs io.files.private windows
9 destructors classes classes.struct specialized-arrays ;
10 SPECIALIZED-ARRAY: ushort
11 SPECIALIZED-ARRAY: void*
12 IN: io.launcher.windows
14 TUPLE: CreateProcess-args
24 lpProcessInformation ;
26 : default-CreateProcess-args ( -- obj )
27 CreateProcess-args new
29 dup class heap-size >>cb
31 PROCESS_INFORMATION <struct> >>lpProcessInformation
32 TRUE >>bInheritHandles
35 : call-CreateProcess ( CreateProcess-args -- )
37 [ lpApplicationName>> ]
39 [ lpProcessAttributes>> ]
40 [ lpThreadAttributes>> ]
44 [ lpCurrentDirectory>> ]
46 [ lpProcessInformation>> ]
48 CreateProcess win32-error=0/f ;
50 : count-trailing-backslashes ( str n -- str n )
51 [ "\\" ?tail ] dip swap [
52 1 + count-trailing-backslashes
55 : fix-trailing-backslashes ( str -- str' )
56 0 count-trailing-backslashes
57 2 * CHAR: \\ <repetition> append ;
59 : escape-argument ( str -- newstr )
60 CHAR: \s over member? [
61 fix-trailing-backslashes "\"" dup surround
64 : join-arguments ( args -- cmd-line )
65 [ escape-argument ] map " " join ;
67 : lookup-priority ( process -- n )
69 { +lowest-priority+ [ IDLE_PRIORITY_CLASS ] }
70 { +low-priority+ [ BELOW_NORMAL_PRIORITY_CLASS ] }
71 { +normal-priority+ [ NORMAL_PRIORITY_CLASS ] }
72 { +high-priority+ [ ABOVE_NORMAL_PRIORITY_CLASS ] }
73 { +highest-priority+ [ HIGH_PRIORITY_CLASS ] }
74 { +realtime-priority+ [ REALTIME_PRIORITY_CLASS ] }
78 : app-name/cmd-line ( process -- app-name cmd-line )
79 command>> dup string? [
82 unclip swap join-arguments
85 : cmd-line ( process -- cmd-line )
86 command>> dup string? [ join-arguments ] unless ;
88 : fill-lpApplicationName ( process args -- process args )
89 over app-name/cmd-line
90 [ >>lpApplicationName ] [ >>lpCommandLine ] bi* ;
92 : fill-lpCommandLine ( process args -- process args )
93 over cmd-line >>lpCommandLine ;
95 : fill-dwCreateFlags ( process args -- process args )
97 pick pass-environment? [ CREATE_UNICODE_ENVIRONMENT bitor ] when
98 pick detached>> os winnt? and [ DETACHED_PROCESS bitor ] when
99 pick lookup-priority [ bitor ] when*
102 : fill-lpEnvironment ( process args -- process args )
103 over pass-environment? [
106 [ swap % "=" % % "\0" % ] assoc-each
108 ] ushort-array{ } make
112 : fill-startup-info ( process args -- process args )
113 dup lpStartupInfo>> STARTF_USESTDHANDLES >>dwFlags drop ;
115 HOOK: fill-redirection io-backend ( process args -- )
117 M: wince fill-redirection 2drop ;
119 : make-CreateProcess-args ( process -- args )
120 default-CreateProcess-args
121 os wince? [ fill-lpApplicationName ] [ fill-lpCommandLine ] if
127 M: windows current-process-handle ( -- handle )
128 GetCurrentProcessId ;
130 M: windows run-process* ( process -- handle )
132 current-directory get absolute-path cd
134 dup make-CreateProcess-args
135 [ fill-redirection ] keep
136 dup call-CreateProcess
137 lpProcessInformation>>
140 M: windows kill-process* ( handle -- )
141 hProcess>> 255 TerminateProcess win32-error=0/f ;
143 : dispose-process ( process-information -- )
144 #! From MSDN: "Handles in PROCESS_INFORMATION must be closed
145 #! with CloseHandle when they are no longer needed."
146 [ hProcess>> [ CloseHandle drop ] when* ]
147 [ hThread>> [ CloseHandle drop ] when* ] bi ;
149 : exit-code ( process -- n )
151 0 <ulong> [ GetExitCodeProcess ] keep *ulong
152 swap win32-error=0/f ;
154 : process-exited ( process -- )
155 dup handle>> exit-code
156 over handle>> dispose-process
159 M: windows wait-for-processes ( -- ? )
160 processes get keys dup
161 [ handle>> hProcess>> ] void*-array{ } map-as
163 WaitForMultipleObjects
164 dup HEX: ffffffff = [ win32-error ] when
165 dup WAIT_TIMEOUT = [ 2drop t ] [ swap nth process-exited f ] if ;