]> gitweb.factorcode.org Git - factor.git/blob - basis/io/launcher/windows/windows.factor
Remove Windows CE from core/ basis/ and build-support/
[factor.git] / basis / io / launcher / windows / windows.factor
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
15
16 TUPLE: CreateProcess-args
17        lpApplicationName
18        lpCommandLine
19        lpProcessAttributes
20        lpThreadAttributes
21        bInheritHandles
22        dwCreateFlags
23        lpEnvironment
24        lpCurrentDirectory
25        lpStartupInfo
26        lpProcessInformation ;
27
28 : default-CreateProcess-args ( -- obj )
29     CreateProcess-args new
30         STARTUPINFO <struct>
31         dup class heap-size >>cb
32     >>lpStartupInfo
33     PROCESS_INFORMATION <struct> >>lpProcessInformation
34     TRUE >>bInheritHandles
35     0 >>dwCreateFlags ;
36
37 : call-CreateProcess ( CreateProcess-args -- )
38     {
39         [ lpApplicationName>> ]
40         [ lpCommandLine>> ]
41         [ lpProcessAttributes>> ]
42         [ lpThreadAttributes>> ]
43         [ bInheritHandles>> ]
44         [ dwCreateFlags>> ]
45         [ lpEnvironment>> ]
46         [ lpCurrentDirectory>> ]
47         [ lpStartupInfo>> ]
48         [ lpProcessInformation>> ]
49     } cleave
50     CreateProcess win32-error=0/f ;
51
52 : count-trailing-backslashes ( str n -- str n )
53     [ "\\" ?tail ] dip swap [
54         1 + count-trailing-backslashes
55     ] when ;
56
57 : fix-trailing-backslashes ( str -- str' )
58     0 count-trailing-backslashes
59     2 * CHAR: \\ <repetition> append ;
60
61 : escape-argument ( str -- newstr )
62     CHAR: \s over member? [
63         fix-trailing-backslashes "\"" dup surround
64     ] when ;
65
66 : join-arguments ( args -- cmd-line )
67     [ escape-argument ] map " " join ;
68
69 : lookup-priority ( process -- n )
70     priority>> {
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 ] }
77         [ drop f ]
78     } case ;
79
80 : cmd-line ( process -- cmd-line )
81     command>> dup string? [ join-arguments ] unless ;
82
83 : fill-lpCommandLine ( process args -- process args )
84     over cmd-line >>lpCommandLine ;
85
86 : fill-dwCreateFlags ( process args -- process args )
87     0
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*
91     >>dwCreateFlags ;
92
93 : fill-lpEnvironment ( process args -- process args )
94     over pass-environment? [
95         [
96             over get-environment
97             [ swap % "=" % % "\0" % ] assoc-each
98             "\0" %
99         ] ushort-array{ } make
100         >>lpEnvironment
101     ] when ;
102
103 : fill-startup-info ( process args -- process args )
104     dup lpStartupInfo>> STARTF_USESTDHANDLES >>dwFlags drop ;
105
106 : make-CreateProcess-args ( process -- args )
107     default-CreateProcess-args
108     fill-lpCommandLine
109     fill-dwCreateFlags
110     fill-lpEnvironment
111     fill-startup-info
112     nip ;
113
114 M: windows current-process-handle ( -- handle )
115     GetCurrentProcessId ;
116
117 ERROR: launch-error process error ;
118
119 M: launch-error error.
120     "Launching failed with error:" print
121     dup error>> error. nl
122     "Launch descriptor:" print nl
123     process>> . ;
124
125 M: windows kill-process* ( handle -- )
126     hProcess>> 255 TerminateProcess win32-error=0/f ;
127
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 ;
133
134 : exit-code ( process -- n )
135     hProcess>>
136     { DWORD } [ GetExitCodeProcess ] with-out-parameters
137     swap win32-error=0/f ;
138
139 : process-exited ( process -- )
140     dup handle>> exit-code
141     over handle>> dispose-process
142     notify-exit ;
143
144 M: windows wait-for-processes ( -- ? )
145     processes get keys dup
146     [ handle>> hProcess>> ] void*-array{ } map-as
147     [ length ] keep 0 0
148     WaitForMultipleObjects
149     dup HEX: ffffffff = [ win32-error ] when
150     dup WAIT_TIMEOUT = [ 2drop t ] [ swap nth process-exited f ] if ;
151
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
159         0 ! options
160         DuplicateHandle win32-error=0/f
161     ] keep void* deref <win32-handle> &dispose ;
162
163 ! /dev/null simulation
164 : null-input ( -- pipe )
165     (pipe) [ in>> &dispose ] [ out>> dispose ] bi ;
166
167 : null-output ( -- pipe )
168     (pipe) [ out>> &dispose ] [ in>> dispose ] bi ;
169
170 : null-pipe ( mode -- pipe )
171     {
172         { GENERIC_READ [ null-input ] }
173         { GENERIC_WRITE [ null-output ] }
174     } case ;
175
176 ! The below code is based on the example given in
177 ! http://msdn2.microsoft.com/en-us/library/ms682499.aspx
178
179 : redirect-default ( obj access-mode create-mode -- handle )
180     3drop f ;
181
182 : redirect-closed ( obj access-mode create-mode -- handle )
183     drop nip null-pipe ;
184
185 :: redirect-file ( path access-mode create-mode -- handle )
186     path normalize-path
187     access-mode
188     share-mode
189     default-security-attributes
190     create-mode
191     FILE_ATTRIBUTE_NORMAL ! flags and attributes
192     f ! template file
193     CreateFile check-invalid-handle <win32-file> &dispose ;
194
195 : redirect-append ( path access-mode create-mode -- handle )
196     [ path>> ] 2dip
197     drop OPEN_ALWAYS
198     redirect-file
199     dup 0 FILE_END set-file-pointer ;
200
201 : redirect-handle ( handle access-mode create-mode -- handle )
202     2drop ;
203
204 : redirect-stream ( stream access-mode create-mode -- handle )
205     [ underlying-handle ] 2dip redirect-handle ;
206
207 : redirect ( obj access-mode create-mode -- handle )
208     {
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 ] }
214         [ redirect-stream ]
215     } cond
216     dup [ dup t set-inherit handle>> ] when ;
217
218 : redirect-stdout ( process args -- handle )
219     drop
220     stdout>>
221     GENERIC_WRITE
222     CREATE_ALWAYS
223     redirect
224     STD_OUTPUT_HANDLE GetStdHandle or ;
225
226 : redirect-stderr ( process args -- handle )
227     over stderr>> +stdout+ eq? [
228         nip
229         lpStartupInfo>> hStdOutput>>
230     ] [
231         drop
232         stderr>>
233         GENERIC_WRITE
234         CREATE_ALWAYS
235         redirect
236         STD_ERROR_HANDLE GetStdHandle or
237     ] if ;
238
239 : redirect-stdin ( process args -- handle )
240     drop
241     stdin>>
242     GENERIC_READ
243     OPEN_EXISTING
244     redirect
245     STD_INPUT_HANDLE GetStdHandle or ;
246     
247 : fill-redirection ( process args -- )
248     dup lpStartupInfo>>
249     [ [ redirect-stdout ] dip hStdOutput<< ]
250     [ [ redirect-stderr ] dip hStdError<< ]
251     [ [ redirect-stdin ] dip hStdInput<< ] 3tri ;
252
253 M: windows run-process* ( process -- handle )
254     [
255         [
256             current-directory get absolute-path cd
257     
258             dup make-CreateProcess-args
259             [ fill-redirection ] keep
260             dup call-CreateProcess
261             lpProcessInformation>>
262         ] with-destructors
263     ] [ launch-error ] recover ;