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.pathnames io.pipes io.pipes.windows io.ports
8 kernel libc locals make math namespaces prettyprint sequences
9 specialized-arrays splitting
10 strings system threads windows windows.errors windows.handles
11 windows.kernel32 windows.types ;
12 SPECIALIZED-ARRAY: ushort
13 SPECIALIZED-ARRAY: void*
14 IN: io.launcher.windows
16 TUPLE: CreateProcess-args
26 lpProcessInformation ;
28 : default-CreateProcess-args ( -- obj )
29 CreateProcess-args new
31 dup class heap-size >>cb
33 PROCESS_INFORMATION <struct> >>lpProcessInformation
34 TRUE >>bInheritHandles
37 : call-CreateProcess ( CreateProcess-args -- )
39 [ lpApplicationName>> ]
41 [ lpProcessAttributes>> ]
42 [ lpThreadAttributes>> ]
46 [ lpCurrentDirectory>> ]
48 [ lpProcessInformation>> ]
50 CreateProcess win32-error=0/f ;
52 : count-trailing-backslashes ( str n -- str n )
53 [ "\\" ?tail ] dip swap [
54 1 + count-trailing-backslashes
57 : fix-trailing-backslashes ( str -- str' )
58 0 count-trailing-backslashes
59 2 * CHAR: \\ <repetition> append ;
61 : escape-argument ( str -- newstr )
62 CHAR: \s over member? [
63 fix-trailing-backslashes "\"" dup surround
66 : join-arguments ( args -- cmd-line )
67 [ escape-argument ] map " " join ;
69 : lookup-priority ( process -- n )
71 { +lowest-priority+ [ IDLE_PRIORITY_CLASS ] }
72 { +low-priority+ [ BELOW_NORMAL_PRIORITY_CLASS ] }
73 { +normal-priority+ [ NORMAL_PRIORITY_CLASS ] }
74 { +high-priority+ [ ABOVE_NORMAL_PRIORITY_CLASS ] }
75 { +highest-priority+ [ HIGH_PRIORITY_CLASS ] }
76 { +realtime-priority+ [ REALTIME_PRIORITY_CLASS ] }
80 : cmd-line ( process -- cmd-line )
81 command>> dup string? [ join-arguments ] unless ;
83 : fill-lpCommandLine ( process args -- process args )
84 over cmd-line >>lpCommandLine ;
86 : fill-dwCreateFlags ( process args -- process args )
88 pick pass-environment? [ CREATE_UNICODE_ENVIRONMENT bitor ] when
89 pick detached>> os windows? and [ DETACHED_PROCESS bitor ] when
90 pick lookup-priority [ bitor ] when*
93 : fill-lpEnvironment ( process args -- process args )
94 over pass-environment? [
97 [ swap % "=" % % "\0" % ] assoc-each
99 ] ushort-array{ } make
103 : fill-startup-info ( process args -- process args )
104 dup lpStartupInfo>> STARTF_USESTDHANDLES >>dwFlags drop ;
106 : make-CreateProcess-args ( process -- args )
107 default-CreateProcess-args
114 M: windows current-process-handle ( -- handle )
115 GetCurrentProcessId ;
117 ERROR: launch-error process error ;
119 M: launch-error error.
120 "Launching failed with error:" print
121 dup error>> error. nl
122 "Launch descriptor:" print nl
125 M: windows kill-process* ( handle -- )
126 hProcess>> 255 TerminateProcess win32-error=0/f ;
128 : dispose-process ( process-information -- )
129 #! From MSDN: "Handles in PROCESS_INFORMATION must be closed
130 #! with CloseHandle when they are no longer needed."
131 [ hProcess>> [ CloseHandle drop ] when* ]
132 [ hThread>> [ CloseHandle drop ] when* ] bi ;
134 : exit-code ( process -- n )
136 { DWORD } [ GetExitCodeProcess ] with-out-parameters
137 swap win32-error=0/f ;
139 : process-exited ( process -- )
140 dup handle>> exit-code
141 over handle>> dispose-process
144 M: windows wait-for-processes ( -- ? )
145 processes get keys dup
146 [ handle>> hProcess>> ] void*-array{ } map-as
148 WaitForMultipleObjects
149 dup HEX: ffffffff = [ win32-error ] when
150 dup WAIT_TIMEOUT = [ 2drop t ] [ swap nth process-exited f ] if ;
152 : duplicate-handle ( handle -- handle' )
153 GetCurrentProcess ! source process
154 swap handle>> ! handle
155 GetCurrentProcess ! target process
156 f void* <ref> [ ! target handle
157 DUPLICATE_SAME_ACCESS ! desired access
158 TRUE ! inherit handle
160 DuplicateHandle win32-error=0/f
161 ] keep void* deref <win32-handle> &dispose ;
163 ! /dev/null simulation
164 : null-input ( -- pipe )
165 (pipe) [ in>> &dispose ] [ out>> dispose ] bi ;
167 : null-output ( -- pipe )
168 (pipe) [ out>> &dispose ] [ in>> dispose ] bi ;
170 : null-pipe ( mode -- pipe )
172 { GENERIC_READ [ null-input ] }
173 { GENERIC_WRITE [ null-output ] }
176 ! The below code is based on the example given in
177 ! http://msdn2.microsoft.com/en-us/library/ms682499.aspx
179 : redirect-default ( obj access-mode create-mode -- handle )
182 : redirect-closed ( obj access-mode create-mode -- handle )
185 :: redirect-file ( path access-mode create-mode -- handle )
189 default-security-attributes
191 FILE_ATTRIBUTE_NORMAL ! flags and attributes
193 CreateFile check-invalid-handle <win32-file> &dispose ;
195 : redirect-append ( path access-mode create-mode -- handle )
199 dup 0 FILE_END set-file-pointer ;
201 : redirect-handle ( handle access-mode create-mode -- handle )
204 : redirect-stream ( stream access-mode create-mode -- handle )
205 [ underlying-handle ] 2dip redirect-handle ;
207 : redirect ( obj access-mode create-mode -- handle )
209 { [ pick not ] [ redirect-default ] }
210 { [ pick +closed+ eq? ] [ redirect-closed ] }
211 { [ pick string? ] [ redirect-file ] }
212 { [ pick appender? ] [ redirect-append ] }
213 { [ pick win32-file? ] [ redirect-handle ] }
216 dup [ dup t set-inherit handle>> ] when ;
218 : redirect-stdout ( process args -- handle )
224 STD_OUTPUT_HANDLE GetStdHandle or ;
226 : redirect-stderr ( process args -- handle )
227 over stderr>> +stdout+ eq? [
229 lpStartupInfo>> hStdOutput>>
236 STD_ERROR_HANDLE GetStdHandle or
239 : redirect-stdin ( process args -- handle )
245 STD_INPUT_HANDLE GetStdHandle or ;
247 : fill-redirection ( process args -- )
249 [ [ redirect-stdout ] dip hStdOutput<< ]
250 [ [ redirect-stderr ] dip hStdError<< ]
251 [ [ redirect-stdin ] dip hStdInput<< ] 3tri ;
253 M: windows run-process* ( process -- handle )
256 current-directory get absolute-path cd
258 dup make-CreateProcess-args
259 [ fill-redirection ] keep
260 dup call-CreateProcess
261 lpProcessInformation>>
263 ] [ launch-error ] recover ;