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
5 math windows.kernel32 windows namespaces io.launcher kernel
6 sequences windows.errors assocs splitting system strings
7 io.windows.launcher io.windows.files
8 io.backend io.files io.files.private combinators shuffle
10 IN: io.windows.nt.launcher
12 : duplicate-handle ( handle -- handle' )
13 GetCurrentProcess ! source process
15 GetCurrentProcess ! target process
16 f <void*> [ ! target handle
17 DUPLICATE_SAME_ACCESS ! desired access
19 DUPLICATE_CLOSE_SOURCE ! options
20 DuplicateHandle win32-error=0/f
23 ! /dev/null simulation
24 : null-input ( -- pipe )
25 (pipe) [ in>> handle>> ] [ out>> dispose ] bi ;
27 : null-output ( -- pipe )
28 (pipe) [ in>> dispose ] [ out>> handle>> ] bi ;
30 : null-pipe ( mode -- pipe )
32 { GENERIC_READ [ null-input ] }
33 { GENERIC_WRITE [ null-output ] }
36 ! The below code is based on the example given in
37 ! http://msdn2.microsoft.com/en-us/library/ms682499.aspx
39 : redirect-default ( obj access-mode create-mode -- handle )
42 : redirect-closed ( obj access-mode create-mode -- handle )
45 :: redirect-file ( path access-mode create-mode -- handle )
49 default-security-attributes
51 FILE_ATTRIBUTE_NORMAL ! flags and attributes
53 CreateFile dup invalid-handle? <win32-file> &dispose handle>> ;
55 : redirect-append ( path access-mode create-mode -- handle )
59 dup 0 FILE_END set-file-pointer ;
61 : redirect-handle ( handle access-mode create-mode -- handle )
62 2drop handle>> duplicate-handle ;
64 : redirect-stream ( stream access-mode create-mode -- handle )
65 >r >r underlying-handle handle>> r> r> redirect-handle ;
67 : redirect ( obj access-mode create-mode -- handle )
69 { [ pick not ] [ redirect-default ] }
70 { [ pick +closed+ eq? ] [ redirect-closed ] }
71 { [ pick string? ] [ redirect-file ] }
72 { [ pick appender? ] [ redirect-append ] }
73 { [ pick win32-file? ] [ redirect-handle ] }
76 dup [ dup t set-inherit ] when ;
78 : redirect-stdout ( process args -- handle )
84 STD_OUTPUT_HANDLE GetStdHandle or ;
86 : redirect-stderr ( process args -- handle )
87 over stderr>> +stdout+ eq? [
89 lpStartupInfo>> STARTUPINFO-hStdOutput
96 STD_ERROR_HANDLE GetStdHandle or
99 : redirect-stdin ( process args -- handle )
105 STD_INPUT_HANDLE GetStdHandle or ;
107 M: winnt fill-redirection ( process args -- )
108 [ 2dup redirect-stdout ] keep lpStartupInfo>> set-STARTUPINFO-hStdOutput
109 [ 2dup redirect-stderr ] keep lpStartupInfo>> set-STARTUPINFO-hStdError
110 [ 2dup redirect-stdin ] keep lpStartupInfo>> set-STARTUPINFO-hStdInput