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.backend.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.launcher.windows io.files.windows io.backend io.files
8 io.files.private combinators shuffle accessors locals ;
9 IN: io.launcher.windows.nt
11 : duplicate-handle ( handle -- handle' )
12 GetCurrentProcess ! source process
13 swap handle>> ! handle
14 GetCurrentProcess ! target process
15 f <void*> [ ! target handle
16 DUPLICATE_SAME_ACCESS ! desired access
19 DuplicateHandle win32-error=0/f
20 ] keep *void* <win32-handle> &dispose ;
22 ! /dev/null simulation
23 : null-input ( -- pipe )
24 (pipe) [ in>> &dispose ] [ out>> dispose ] bi ;
26 : null-output ( -- pipe )
27 (pipe) [ in>> dispose ] [ out>> &dispose ] 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 ;
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 )
63 : redirect-stream ( stream access-mode create-mode -- handle )
64 [ underlying-handle ] 2dip 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 handle>> ] 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>> 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 -- )
108 [ [ redirect-stdout ] dip hStdOutput<< ]
109 [ [ redirect-stderr ] dip hStdError<< ]
110 [ [ redirect-stdin ] dip hStdInput<< ] 3tri ;