]> gitweb.factorcode.org Git - factor.git/blob - basis/io/windows/nt/launcher/launcher.factor
Fix permission bits
[factor.git] / basis / io / windows / nt / launcher / launcher.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.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
10
11 : duplicate-handle ( handle -- handle' )
12     GetCurrentProcess ! source process
13     swap ! handle
14     GetCurrentProcess ! target process
15     f <void*> [ ! target handle
16         DUPLICATE_SAME_ACCESS ! desired access
17         TRUE ! inherit handle
18         DUPLICATE_CLOSE_SOURCE ! options
19         DuplicateHandle win32-error=0/f
20     ] keep *void* ;
21
22 ! /dev/null simulation
23 : null-input ( -- pipe )
24     (pipe) [ in>> handle>> ] [ out>> dispose ] bi ;
25
26 : null-output ( -- pipe )
27     (pipe) [ in>> dispose ] [ out>> handle>> ] 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 handle>> ;
53
54 : redirect-append ( path access-mode create-mode -- handle )
55     >r >r path>> r> r>
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 handle>> duplicate-handle ;
62
63 : redirect-stream ( stream access-mode create-mode -- handle )
64     >r >r underlying-handle handle>> r> r> 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 ] 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>> STARTUPINFO-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     [ 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
110     2drop ;