1 ! Copyright (C) 2007, 2010 Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data arrays assocs
4 classes classes.struct combinators concurrency.flags
5 continuations debugger destructors init io io.backend
6 io.backend.windows io.files io.files.private io.files.windows
7 io.launcher io.launcher.private io.pathnames io.pipes
8 io.pipes.windows io.ports kernel libc locals make math
9 namespaces prettyprint sequences specialized-arrays splitting
10 splitting.monotonic strings system threads windows
11 windows.errors windows.handles windows.kernel32 windows.types
12 combinators.short-circuit ;
13 SPECIALIZED-ARRAY: ushort
14 SPECIALIZED-ARRAY: void*
15 IN: io.launcher.windows
17 TUPLE: CreateProcess-args
27 lpProcessInformation ;
29 : default-CreateProcess-args ( -- obj )
30 CreateProcess-args new
32 dup class-of heap-size >>cb
34 PROCESS_INFORMATION <struct> >>lpProcessInformation
35 TRUE >>bInheritHandles
38 : call-CreateProcess ( CreateProcess-args -- )
40 [ lpApplicationName>> ]
42 [ lpProcessAttributes>> ]
43 [ lpThreadAttributes>> ]
47 [ lpCurrentDirectory>> ]
49 [ lpProcessInformation>> ]
51 CreateProcess win32-error=0/f ;
53 : count-trailing-backslashes ( str n -- str n )
54 [ "\\" ?tail ] dip swap [
55 1 + count-trailing-backslashes
58 : fix-trailing-backslashes ( str -- str' )
59 0 count-trailing-backslashes
60 2 * CHAR: \\ <repetition> append ;
62 ! Find groups of \, groups of \ followed by ", or naked "
63 : escape-double-quote ( str -- newstr )
65 { [ drop CHAR: \ = ] [ nip "\\\"" member? ] } 2&&
69 ! String of backslashes + double-quote
70 length 1 - 2 * CHAR: \ <repetition> "\\\"" append
78 ! Naked double-quotes get a backslash before them
79 ! Backslashes before a double-quote get doubled in the output
80 ! If there's a space, double trailing backslashes and surround by quotes
81 ! See http://msdn.microsoft.com/en-us/library/ms647232.aspx
82 : escape-argument ( str -- newstr )
84 CHAR: \s over member? [
85 fix-trailing-backslashes "\"" dup surround
88 : join-arguments ( args -- cmd-line )
89 [ escape-argument ] map " " join ;
91 : lookup-priority ( process -- n )
93 { +lowest-priority+ [ IDLE_PRIORITY_CLASS ] }
94 { +low-priority+ [ BELOW_NORMAL_PRIORITY_CLASS ] }
95 { +normal-priority+ [ NORMAL_PRIORITY_CLASS ] }
96 { +high-priority+ [ ABOVE_NORMAL_PRIORITY_CLASS ] }
97 { +highest-priority+ [ HIGH_PRIORITY_CLASS ] }
98 { +realtime-priority+ [ REALTIME_PRIORITY_CLASS ] }
102 : cmd-line ( process -- cmd-line )
103 command>> dup string? [ join-arguments ] unless ;
105 : fill-lpCommandLine ( process args -- process args )
106 over cmd-line >>lpCommandLine ;
108 : fill-dwCreateFlags ( process args -- process args )
110 pick pass-environment? [ CREATE_UNICODE_ENVIRONMENT bitor ] when
111 pick detached>> os windows? and [ DETACHED_PROCESS bitor ] when
112 pick lookup-priority [ bitor ] when*
115 : fill-lpEnvironment ( process args -- process args )
116 over pass-environment? [
119 [ swap % "=" % % "\0" % ] assoc-each
121 ] ushort-array{ } make
125 : fill-startup-info ( process args -- process args )
126 dup lpStartupInfo>> STARTF_USESTDHANDLES >>dwFlags drop ;
128 : make-CreateProcess-args ( process -- args )
129 default-CreateProcess-args
136 M: windows (current-process) ( -- handle )
137 GetCurrentProcessId ;
139 ERROR: launch-error process error ;
141 M: launch-error error.
142 "Launching failed with error:" print
143 dup error>> error. nl
144 "Launch descriptor:" print nl
147 M: windows (kill-process) ( process -- )
148 handle>> hProcess>> 255 TerminateProcess win32-error=0/f ;
150 : dispose-process ( process-information -- )
151 #! From MSDN: "Handles in PROCESS_INFORMATION must be closed
152 #! with CloseHandle when they are no longer needed."
153 [ hProcess>> [ CloseHandle drop ] when* ]
154 [ hThread>> [ CloseHandle drop ] when* ] bi ;
156 : exit-code ( process -- n )
158 { DWORD } [ GetExitCodeProcess ] with-out-parameters
159 swap win32-error=0/f ;
161 : process-exited ( process -- )
162 dup handle>> exit-code
163 over handle>> dispose-process
166 M: windows (wait-for-processes) ( -- ? )
167 processes get keys dup
168 [ handle>> hProcess>> ] void*-array{ } map-as
170 WaitForMultipleObjects
171 dup 0xffffffff = [ win32-error ] when
172 dup WAIT_TIMEOUT = [ 2drop t ] [ swap nth process-exited f ] if ;
174 : duplicate-handle ( handle -- handle' )
175 GetCurrentProcess ! source process
176 swap handle>> ! handle
177 GetCurrentProcess ! target process
178 f void* <ref> [ ! target handle
179 DUPLICATE_SAME_ACCESS ! desired access
180 TRUE ! inherit handle
182 DuplicateHandle win32-error=0/f
183 ] keep void* deref <win32-handle> &dispose ;
185 ! /dev/null simulation
186 : null-input ( -- pipe )
187 (pipe) [ in>> &dispose ] [ out>> dispose ] bi ;
189 : null-output ( -- pipe )
190 (pipe) [ out>> &dispose ] [ in>> dispose ] bi ;
192 : null-pipe ( mode -- pipe )
194 { GENERIC_READ [ null-input ] }
195 { GENERIC_WRITE [ null-output ] }
198 ! The below code is based on the example given in
199 ! http://msdn2.microsoft.com/en-us/library/ms682499.aspx
201 : redirect-default ( obj access-mode create-mode -- handle )
204 : redirect-closed ( obj access-mode create-mode -- handle )
207 :: redirect-file ( path access-mode create-mode -- handle )
211 default-security-attributes
213 FILE_ATTRIBUTE_NORMAL ! flags and attributes
215 CreateFile check-invalid-handle <win32-file> &dispose ;
217 : redirect-append ( path access-mode create-mode -- handle )
221 dup 0 FILE_END set-file-pointer ;
223 : redirect-handle ( handle access-mode create-mode -- handle )
226 : redirect-stream ( stream access-mode create-mode -- handle )
227 [ underlying-handle ] 2dip redirect-handle ;
229 : redirect ( obj access-mode create-mode -- handle )
231 { [ pick not ] [ redirect-default ] }
232 { [ pick +closed+ eq? ] [ redirect-closed ] }
233 { [ pick string? ] [ redirect-file ] }
234 { [ pick appender? ] [ redirect-append ] }
235 { [ pick win32-file? ] [ redirect-handle ] }
238 dup [ dup t set-inherit handle>> ] when ;
240 : redirect-stdout ( process args -- handle )
246 STD_OUTPUT_HANDLE GetStdHandle or ;
248 : redirect-stderr ( process args -- handle )
249 over stderr>> +stdout+ eq? [
251 lpStartupInfo>> hStdOutput>>
258 STD_ERROR_HANDLE GetStdHandle or
261 : redirect-stdin ( process args -- handle )
267 STD_INPUT_HANDLE GetStdHandle or ;
269 : fill-redirection ( process args -- )
271 [ [ redirect-stdout ] dip hStdOutput<< ]
272 [ [ redirect-stderr ] dip hStdError<< ]
273 [ [ redirect-stdin ] dip hStdInput<< ] 3tri ;
275 M: windows (run-process) ( process -- handle )
278 dup make-CreateProcess-args
279 current-directory get absolute-path >>lpCurrentDirectory
280 [ fill-redirection ] keep
281 dup call-CreateProcess
282 lpProcessInformation>>
284 ] [ throw-launch-error ] recover ;