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 destructors io
4 io.windows libc io.ports io.pipes windows.types math
5 windows.kernel32 windows namespaces make io.launcher kernel
6 sequences windows.errors assocs splitting system strings
7 io.windows.launcher io.windows.files io.backend io.files
8 io.files.private combinators shuffle accessors locals ;
9 IN: io.windows.nt.launcher
11 : duplicate-handle ( handle -- handle' )
12 GetCurrentProcess ! source process
14 GetCurrentProcess ! target process
15 f <void*> [ ! target handle
16 DUPLICATE_SAME_ACCESS ! desired access
18 DUPLICATE_CLOSE_SOURCE ! options
19 DuplicateHandle win32-error=0/f
22 ! /dev/null simulation
23 : null-input ( -- pipe )
24 (pipe) [ in>> handle>> ] [ out>> dispose ] bi ;
26 : null-output ( -- pipe )
27 (pipe) [ in>> dispose ] [ out>> handle>> ] bi ;
29 : null-pipe ( mode -- pipe )
31 { GENERIC_READ [ null-input ] }
32 { GENERIC_WRITE [ null-output ] }
35 ! The below code is based on the example given in
36 ! http://msdn2.microsoft.com/en-us/library/ms682499.aspx
38 : redirect-default ( obj access-mode create-mode -- handle )
41 : redirect-closed ( obj access-mode create-mode -- handle )
44 :: redirect-file ( path access-mode create-mode -- handle )
48 default-security-attributes
50 FILE_ATTRIBUTE_NORMAL ! flags and attributes
52 CreateFile dup invalid-handle? <win32-file> &dispose handle>> ;
54 : redirect-append ( path access-mode create-mode -- handle )
58 dup 0 FILE_END set-file-pointer ;
60 : redirect-handle ( handle access-mode create-mode -- handle )
61 2drop handle>> duplicate-handle ;
63 : redirect-stream ( stream access-mode create-mode -- handle )
64 >r >r underlying-handle handle>> r> r> redirect-handle ;
66 : redirect ( obj access-mode create-mode -- handle )
68 { [ pick not ] [ redirect-default ] }
69 { [ pick +closed+ eq? ] [ redirect-closed ] }
70 { [ pick string? ] [ redirect-file ] }
71 { [ pick appender? ] [ redirect-append ] }
72 { [ pick win32-file? ] [ redirect-handle ] }
75 dup [ dup t set-inherit ] when ;
77 : redirect-stdout ( process args -- handle )
83 STD_OUTPUT_HANDLE GetStdHandle or ;
85 : redirect-stderr ( process args -- handle )
86 over stderr>> +stdout+ eq? [
88 lpStartupInfo>> STARTUPINFO-hStdOutput
95 STD_ERROR_HANDLE GetStdHandle or
98 : redirect-stdin ( process args -- handle )
104 STD_INPUT_HANDLE GetStdHandle or ;
106 M: winnt fill-redirection ( process args -- )
107 [ 2dup redirect-stdout ] keep lpStartupInfo>> set-STARTUPINFO-hStdOutput
108 [ 2dup redirect-stderr ] keep lpStartupInfo>> set-STARTUPINFO-hStdError
109 [ 2dup redirect-stdin ] keep lpStartupInfo>> set-STARTUPINFO-hStdInput