1 ! Copyright (C) 2007, 2010 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 debugger prettyprint ;
11 SPECIALIZED-ARRAY: ushort
12 SPECIALIZED-ARRAY: void*
13 IN: io.launcher.windows
15 TUPLE: CreateProcess-args
25 lpProcessInformation ;
27 : default-CreateProcess-args ( -- obj )
28 CreateProcess-args new
30 dup class heap-size >>cb
32 PROCESS_INFORMATION <struct> >>lpProcessInformation
33 TRUE >>bInheritHandles
36 : call-CreateProcess ( CreateProcess-args -- )
38 [ lpApplicationName>> ]
40 [ lpProcessAttributes>> ]
41 [ lpThreadAttributes>> ]
45 [ lpCurrentDirectory>> ]
47 [ lpProcessInformation>> ]
49 CreateProcess win32-error=0/f ;
51 : count-trailing-backslashes ( str n -- str n )
52 [ "\\" ?tail ] dip swap [
53 1 + count-trailing-backslashes
56 : fix-trailing-backslashes ( str -- str' )
57 0 count-trailing-backslashes
58 2 * CHAR: \\ <repetition> append ;
60 : escape-argument ( str -- newstr )
61 CHAR: \s over member? [
62 fix-trailing-backslashes "\"" dup surround
65 : join-arguments ( args -- cmd-line )
66 [ escape-argument ] map " " join ;
68 : lookup-priority ( process -- n )
70 { +lowest-priority+ [ IDLE_PRIORITY_CLASS ] }
71 { +low-priority+ [ BELOW_NORMAL_PRIORITY_CLASS ] }
72 { +normal-priority+ [ NORMAL_PRIORITY_CLASS ] }
73 { +high-priority+ [ ABOVE_NORMAL_PRIORITY_CLASS ] }
74 { +highest-priority+ [ HIGH_PRIORITY_CLASS ] }
75 { +realtime-priority+ [ REALTIME_PRIORITY_CLASS ] }
79 : app-name/cmd-line ( process -- app-name cmd-line )
80 command>> dup string? [
83 unclip swap join-arguments
86 : cmd-line ( process -- cmd-line )
87 command>> dup string? [ join-arguments ] unless ;
89 : fill-lpApplicationName ( process args -- process args )
90 over app-name/cmd-line
91 [ >>lpApplicationName ] [ >>lpCommandLine ] bi* ;
93 : fill-lpCommandLine ( process args -- process args )
94 over cmd-line >>lpCommandLine ;
96 : fill-dwCreateFlags ( process args -- process args )
98 pick pass-environment? [ CREATE_UNICODE_ENVIRONMENT bitor ] when
99 pick detached>> os winnt? and [ DETACHED_PROCESS bitor ] when
100 pick lookup-priority [ bitor ] when*
103 : fill-lpEnvironment ( process args -- process args )
104 over pass-environment? [
107 [ swap % "=" % % "\0" % ] assoc-each
109 ] ushort-array{ } make
113 : fill-startup-info ( process args -- process args )
114 dup lpStartupInfo>> STARTF_USESTDHANDLES >>dwFlags drop ;
116 HOOK: fill-redirection io-backend ( process args -- )
118 M: wince fill-redirection 2drop ;
120 : make-CreateProcess-args ( process -- args )
121 default-CreateProcess-args
122 os wince? [ fill-lpApplicationName ] [ fill-lpCommandLine ] if
128 M: windows current-process-handle ( -- handle )
129 GetCurrentProcessId ;
131 ERROR: launch-error process error ;
133 M: launch-error error.
134 "Launching failed with error:" print
135 dup error>> error. nl
136 "Launch descriptor:" print nl
139 M: windows run-process* ( process -- handle )
142 current-directory get absolute-path cd
144 dup make-CreateProcess-args
145 [ fill-redirection ] keep
146 dup call-CreateProcess
147 lpProcessInformation>>
149 ] [ launch-error ] recover ;
151 M: windows kill-process* ( handle -- )
152 hProcess>> 255 TerminateProcess win32-error=0/f ;
154 : dispose-process ( process-information -- )
155 #! From MSDN: "Handles in PROCESS_INFORMATION must be closed
156 #! with CloseHandle when they are no longer needed."
157 [ hProcess>> [ CloseHandle drop ] when* ]
158 [ hThread>> [ CloseHandle drop ] when* ] bi ;
160 : exit-code ( process -- n )
162 { DWORD } [ GetExitCodeProcess ] [ ] with-out-parameters
163 swap win32-error=0/f ;
165 : process-exited ( process -- )
166 dup handle>> exit-code
167 over handle>> dispose-process
170 M: windows wait-for-processes ( -- ? )
171 processes get keys dup
172 [ handle>> hProcess>> ] void*-array{ } map-as
174 WaitForMultipleObjects
175 dup HEX: ffffffff = [ win32-error ] when
176 dup WAIT_TIMEOUT = [ 2drop t ] [ swap nth process-exited f ] if ;