]> gitweb.factorcode.org Git - factor.git/blob - basis/io/launcher/windows/nt/nt.factor
959bf931199665bd1e0420de290e3554a9b64ce9
[factor.git] / basis / io / launcher / windows / nt / nt.factor
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
10
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
17         TRUE ! inherit handle
18         0 ! options
19         DuplicateHandle win32-error=0/f
20     ] keep *void* <win32-handle> &dispose ;
21
22 ! /dev/null simulation
23 : null-input ( -- pipe )
24     (pipe) [ in>> &dispose ] [ out>> dispose ] bi ;
25
26 : null-output ( -- pipe )
27     (pipe) [ in>> dispose ] [ out>> &dispose ] bi ;
28
29 : null-pipe ( mode -- pipe )
30     {
31         { GENERIC_READ [ null-input ] }
32         { GENERIC_WRITE [ null-output ] }
33     } case ;
34
35 ! The below code is based on the example given in
36 ! http://msdn2.microsoft.com/en-us/library/ms682499.aspx
37
38 : redirect-default ( obj access-mode create-mode -- handle )
39     3drop f ;
40
41 : redirect-closed ( obj access-mode create-mode -- handle )
42     drop nip null-pipe ;
43
44 :: redirect-file ( path access-mode create-mode -- handle )
45     path normalize-path
46     access-mode
47     share-mode
48     default-security-attributes
49     create-mode
50     FILE_ATTRIBUTE_NORMAL ! flags and attributes
51     f ! template file
52     CreateFile dup invalid-handle? <win32-file> &dispose ;
53
54 : redirect-append ( path access-mode create-mode -- handle )
55     [ path>> ] 2dip
56     drop OPEN_ALWAYS
57     redirect-file
58     dup 0 FILE_END set-file-pointer ;
59
60 : redirect-handle ( handle access-mode create-mode -- handle )
61     2drop ;
62
63 : redirect-stream ( stream access-mode create-mode -- handle )
64     [ underlying-handle ] 2dip redirect-handle ;
65
66 : redirect ( obj access-mode create-mode -- handle )
67     {
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 ] }
73         [ redirect-stream ]
74     } cond
75     dup [ dup t set-inherit handle>> ] when ;
76
77 : redirect-stdout ( process args -- handle )
78     drop
79     stdout>>
80     GENERIC_WRITE
81     CREATE_ALWAYS
82     redirect
83     STD_OUTPUT_HANDLE GetStdHandle or ;
84
85 : redirect-stderr ( process args -- handle )
86     over stderr>> +stdout+ eq? [
87         nip
88         lpStartupInfo>> hStdOutput>>
89     ] [
90         drop
91         stderr>>
92         GENERIC_WRITE
93         CREATE_ALWAYS
94         redirect
95         STD_ERROR_HANDLE GetStdHandle or
96     ] if ;
97
98 : redirect-stdin ( process args -- handle )
99     drop
100     stdin>>
101     GENERIC_READ
102     OPEN_EXISTING
103     redirect
104     STD_INPUT_HANDLE GetStdHandle or ;
105
106 M: winnt fill-redirection ( process args -- )
107     dup lpStartupInfo>>
108     [ [ redirect-stdout ] dip hStdOutput<< ]
109     [ [ redirect-stderr ] dip hStdError<< ]
110     [ [ redirect-stdin ] dip hStdInput<< ] 3tri ;