442
|
1 /* I-connector utility
|
|
2 Copyright (C) 2000 Kirill M. Katsnelson
|
1346
|
3 Copyright (C) 2002, 2003 Ben Wing.
|
442
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* When run with an argument, i treats it as a command line, and pipes
|
|
23 command stdin, stdout and stderr to its own respective streams. How
|
|
24 silly it should sound, but windowed program in Win32 cannot do output
|
|
25 to the console from which it has been started, and should be run using
|
|
26 this utility.
|
|
27
|
|
28 This utility is for running [tx]emacs as part of make process so that
|
|
29 its output goes to the same console as the rest of the make output
|
|
30 does. It can be used also when xemacs should be run as a batch
|
|
31 command ina script, especially when its standart output should be
|
|
32 obtained programmatically. */
|
|
33
|
|
34 #include <windows.h>
|
|
35 #include <stdio.h>
|
|
36 #include <string.h>
|
|
37 #include <tchar.h>
|
|
38
|
|
39 typedef struct
|
|
40 {
|
|
41 HANDLE source;
|
|
42 HANDLE drain;
|
|
43 } I_connector;
|
|
44
|
|
45 /*
|
|
46 * Make new handle as that pointed to by PH but
|
|
47 * inheritable, substitute PH with it, and close the
|
|
48 * original one
|
|
49 */
|
|
50 static void
|
|
51 make_inheritable (HANDLE* ph)
|
|
52 {
|
|
53 HANDLE htmp;
|
|
54 DuplicateHandle (GetCurrentProcess(), *ph, GetCurrentProcess(), &htmp,
|
|
55 0, TRUE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
56 *ph = htmp;
|
|
57 }
|
|
58
|
|
59 /*
|
|
60 * Worker thread proc. Reads source, pumps into drain,
|
|
61 * till either clogs.
|
|
62 */
|
|
63 static DWORD CALLBACK
|
|
64 pump (LPVOID pv_i)
|
|
65 {
|
|
66 I_connector* pi = (I_connector*) pv_i;
|
|
67 BYTE buffer [256];
|
|
68 DWORD really_read, unused;
|
|
69
|
1346
|
70 /* The docs for ReadFile claim:
|
|
71
|
|
72 The ReadFile function returns when one of the following is true: a write
|
|
73 operation completes on the write end of the pipe, the number of bytes
|
|
74 requested has been read, or an error occurs.
|
|
75
|
|
76 But this is just not true. ReadFile never seems to block, and unless we
|
|
77 Sleep(), we will chew up all the CPU time. --ben
|
|
78 */
|
442
|
79 while (ReadFile (pi->source, buffer, sizeof (buffer), &really_read, NULL) &&
|
|
80 WriteFile (pi->drain, buffer, really_read, &unused, NULL))
|
1346
|
81 Sleep (100);
|
442
|
82
|
|
83 return 0;
|
|
84 }
|
|
85
|
|
86 /*
|
|
87 * Launch a pump for the given I-connector
|
|
88 */
|
|
89 static void
|
|
90 start_pump (I_connector* pi)
|
|
91 {
|
|
92 DWORD unused;
|
|
93 HANDLE h_thread = CreateThread (NULL, 0, pump, (void*)pi, 0, &unused);
|
|
94 CloseHandle (h_thread);
|
|
95 }
|
|
96
|
826
|
97 static HANDLE external_event;
|
|
98
|
|
99 static BOOL
|
|
100 ctrl_c_handler (unsigned long type)
|
|
101 {
|
|
102 SetEvent (external_event);
|
|
103 return FALSE;
|
|
104 }
|
|
105
|
|
106 /* Skip over the executable name in the given command line. Correctly
|
|
107 handles quotes in the name. Return NULL upon error. If
|
|
108 REQUIRE_FOLLOWING is non-zero, it's an error if no argument follows the
|
|
109 executable name. */
|
|
110
|
442
|
111 static LPTSTR
|
826
|
112 skip_executable_name (LPTSTR cl, int require_following)
|
442
|
113 {
|
|
114 int ix;
|
|
115
|
|
116 while (1)
|
|
117 {
|
|
118 ix = _tcscspn (cl, _T(" \t\""));
|
|
119 if (cl[ix] == '\"')
|
|
120 {
|
|
121 cl = _tcschr (cl + ix + 1, '\"');
|
|
122 if (cl == NULL)
|
|
123 return NULL; /* Unmatched quote */
|
|
124 cl++;
|
|
125 }
|
|
126 else
|
|
127 {
|
|
128 cl += ix;
|
|
129 cl += _tcsspn (cl, _T(" \t"));
|
826
|
130 if (!require_following)
|
|
131 return cl;
|
442
|
132 return *cl ? cl : NULL;
|
|
133 }
|
|
134 }
|
|
135 }
|
|
136
|
|
137 /*
|
|
138 * Brew coffee and bring snickers
|
|
139 */
|
|
140 void
|
|
141 usage (void)
|
|
142 {
|
|
143 fprintf (stderr,
|
|
144 "\n"
|
|
145 "usage: i command\n"
|
|
146 "i executes the command and reroutes its standard handles to the calling\n"
|
|
147 "console. Good for seeing output of GUI programs that use standard output."
|
|
148 "\n");
|
|
149 }
|
|
150
|
|
151 int
|
|
152 main (void)
|
|
153 {
|
|
154 STARTUPINFO si;
|
|
155 PROCESS_INFORMATION pi;
|
|
156 I_connector I_in, I_out, I_err;
|
|
157 DWORD exit_code;
|
826
|
158 LPTSTR command = skip_executable_name (GetCommandLine (), 1);
|
|
159
|
442
|
160 if (command == NULL)
|
|
161 {
|
|
162 usage ();
|
|
163 return 1;
|
|
164 }
|
|
165
|
|
166 ZeroMemory (&si, sizeof (si));
|
|
167 si.dwFlags = STARTF_USESTDHANDLES;
|
|
168
|
|
169 I_in.source = GetStdHandle (STD_INPUT_HANDLE);
|
|
170 CreatePipe (&si.hStdInput, &I_in.drain, NULL, 0);
|
|
171 make_inheritable (&si.hStdInput);
|
|
172
|
|
173 I_out.drain = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
174 CreatePipe (&I_out.source, &si.hStdOutput, NULL, 0);
|
|
175 make_inheritable (&si.hStdOutput);
|
|
176
|
|
177 I_err.drain = GetStdHandle (STD_ERROR_HANDLE);
|
|
178 CreatePipe (&I_err.source, &si.hStdError, NULL, 0);
|
|
179 make_inheritable (&si.hStdError);
|
|
180
|
826
|
181 {
|
|
182 SECURITY_ATTRIBUTES sa;
|
|
183 LPTSTR new_command =
|
|
184 (LPTSTR) malloc (666 + sizeof (TCHAR) * _tcslen (command));
|
|
185 LPTSTR past_exe;
|
|
186
|
|
187 if (!new_command)
|
|
188 {
|
|
189 _ftprintf (stderr, _T ("Out of memory when launching `%s'\n"),
|
|
190 command);
|
|
191 return 2;
|
|
192 }
|
|
193
|
|
194 past_exe = skip_executable_name (command, 0);
|
|
195 if (!past_exe)
|
|
196 {
|
|
197 usage ();
|
|
198 return 1;
|
|
199 }
|
|
200
|
|
201 /* Since XEmacs isn't a console application, it can't easily be
|
|
202 terminated using ^C. Therefore, we set up a communication path with
|
|
203 it so that when a ^C is sent to us (using GenerateConsoleCtrlEvent),
|
|
204 we in turn signals it to commit suicide. (This is cleaner than using
|
|
205 TerminateProcess()). This makes (e.g.) the "Stop Build" command
|
|
206 from VC++ correctly terminate XEmacs.
|
|
207
|
|
208 #### This will cause problems if i.exe is used for commands other
|
|
209 than XEmacs. We need to make behavior this a command-line
|
|
210 option. */
|
442
|
211
|
826
|
212 /* Create the event as inheritable so that we can use it to communicate
|
|
213 with the child process */
|
|
214 sa.nLength = sizeof (sa);
|
|
215 sa.bInheritHandle = TRUE;
|
|
216 sa.lpSecurityDescriptor = NULL;
|
|
217 external_event = CreateEvent (&sa, FALSE, FALSE, NULL);
|
|
218 if (!external_event)
|
|
219 {
|
|
220 _ftprintf (stderr, _T ("Error %d creating signal event for `%s'\n"),
|
|
221 GetLastError (), command);
|
|
222 return 2;
|
|
223 }
|
|
224
|
|
225 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
|
|
226 _tcsncpy (new_command, command, past_exe - command);
|
|
227 _stprintf (new_command + (past_exe - command),
|
|
228 /* start with space in case no args past command name */
|
|
229 " -mswindows-termination-handle %d ", (long) external_event);
|
|
230 _tcscat (new_command, past_exe);
|
|
231
|
|
232 if (CreateProcess (NULL, new_command, NULL, NULL, TRUE, 0,
|
|
233 NULL, NULL, &si, &pi) == 0)
|
|
234 {
|
|
235 _ftprintf (stderr, _T("Error %d launching `%s'\n"),
|
|
236 GetLastError (), command);
|
|
237 return 2;
|
|
238 }
|
|
239
|
|
240 CloseHandle (pi.hThread);
|
|
241 }
|
|
242
|
442
|
243
|
|
244 /* Start pump in each I-connector */
|
|
245 start_pump (&I_in);
|
|
246 start_pump (&I_out);
|
|
247 start_pump (&I_err);
|
|
248
|
|
249 /* Wait for the process to complete */
|
|
250 WaitForSingleObject (pi.hProcess, INFINITE);
|
|
251 GetExitCodeProcess (pi.hProcess, &exit_code);
|
|
252 CloseHandle (pi.hProcess);
|
|
253
|
|
254 /* Make pump threads eventually die out. Looks rude, I agree */
|
|
255 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
|
|
256 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
|
|
257 CloseHandle (GetStdHandle (STD_ERROR_HANDLE));
|
|
258
|
|
259 return exit_code;
|
|
260 }
|