annotate lib-src/i.c @ 406:b8cc9ab3f761 r21-2-33

Import from CVS: tag r21-2-33
author cvs
date Mon, 13 Aug 2007 11:17:09 +0200
parents
children 501cfd01ee6d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
1 /* I-connector utility
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
2 Copyright (C) 2000 Kirill M. Katsnelson
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
3
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
4 This file is part of XEmacs.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
5
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
9 later version.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
10
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
14 for more details.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
15
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
20
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
21 /* When run with an argument, i treats it as a command line, and pipes
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
22 command stdin, stdout and stderr to its own respective streams. How
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
23 silly it should sound, but windowed program in Win32 cannot do output
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
24 to the console from which it has been started, and should be run using
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
25 this utility.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
26
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
27 This utility is for running [tx]emacs as part of make process so that
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
28 its output goes to the same console as the rest of the make output
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
29 does. It can be used also when xemacs should be run as a batch
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
30 command ina script, especially when its standart output should be
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
31 obtained programmatically. */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
32
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
33 #include <windows.h>
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
34 #include <stdio.h>
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
35 #include <string.h>
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
36 #include <tchar.h>
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
37
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
38 typedef struct
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
39 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
40 HANDLE source;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
41 HANDLE drain;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
42 } I_connector;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
43
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
44 /*
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
45 * Make new handle as that pointed to by PH but
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
46 * inheritable, substitute PH with it, and close the
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
47 * original one
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
48 */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
49 static void
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
50 make_inheritable (HANDLE* ph)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
51 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
52 HANDLE htmp;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
53 DuplicateHandle (GetCurrentProcess(), *ph, GetCurrentProcess(), &htmp,
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
54 0, TRUE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
55 *ph = htmp;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
56 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
57
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
58 /*
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
59 * Worker thread proc. Reads source, pumps into drain,
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
60 * till either clogs.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
61 */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
62 static DWORD CALLBACK
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
63 pump (LPVOID pv_i)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
64 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
65 I_connector* pi = (I_connector*) pv_i;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
66 BYTE buffer [256];
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
67 DWORD really_read, unused;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
68
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
69 while (ReadFile (pi->source, buffer, sizeof (buffer), &really_read, NULL) &&
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
70 WriteFile (pi->drain, buffer, really_read, &unused, NULL))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
71 ;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
72
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
73 return 0;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
74 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
75
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
76 /*
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
77 * Launch a pump for the given I-connector
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
78 */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
79 static void
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
80 start_pump (I_connector* pi)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
81 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
82 DWORD unused;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
83 HANDLE h_thread = CreateThread (NULL, 0, pump, (void*)pi, 0, &unused);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
84 CloseHandle (h_thread);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
85 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
86
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
87 /*
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
88 * Get command line, skip over the executable name, return the rest.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
89 */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
90 static LPTSTR
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
91 get_command (void)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
92 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
93 LPTSTR q, ws, cl = GetCommandLine ();
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
94 int ix;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
95
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
96 while (1)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
97 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
98 ix = _tcscspn (cl, _T(" \t\""));
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
99 if (cl[ix] == '\"')
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
100 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
101 cl = _tcschr (cl + ix + 1, '\"');
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
102 if (cl == NULL)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
103 return NULL; /* Unmatched quote */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
104 cl++;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
105 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
106 else
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
107 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
108 cl += ix;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
109 cl += _tcsspn (cl, _T(" \t"));
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
110 return *cl ? cl : NULL;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
111 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
112 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
113 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
114
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
115 /*
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
116 * Brew coffee and bring snickers
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
117 */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
118 void
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
119 usage (void)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
120 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
121 fprintf (stderr,
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
122 "\n"
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
123 "usage: i command\n"
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
124 "i executes the command and reroutes its standard handles to the calling\n"
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
125 "console. Good for seeing output of GUI programs that use standard output."
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
126 "\n");
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
127 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
128
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
129 int
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
130 main (void)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
131 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
132 STARTUPINFO si;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
133 PROCESS_INFORMATION pi;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
134 I_connector I_in, I_out, I_err;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
135 DWORD exit_code;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
136
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
137 LPTSTR command = get_command ();
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
138 if (command == NULL)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
139 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
140 usage ();
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
141 return 1;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
142 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
143
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
144 ZeroMemory (&si, sizeof (si));
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
145 si.dwFlags = STARTF_USESTDHANDLES;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
146
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
147 I_in.source = GetStdHandle (STD_INPUT_HANDLE);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
148 CreatePipe (&si.hStdInput, &I_in.drain, NULL, 0);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
149 make_inheritable (&si.hStdInput);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
150
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
151 I_out.drain = GetStdHandle (STD_OUTPUT_HANDLE);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
152 CreatePipe (&I_out.source, &si.hStdOutput, NULL, 0);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
153 make_inheritable (&si.hStdOutput);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
154
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
155 I_err.drain = GetStdHandle (STD_ERROR_HANDLE);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
156 CreatePipe (&I_err.source, &si.hStdError, NULL, 0);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
157 make_inheritable (&si.hStdError);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
158
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
159 if (CreateProcess (NULL, command, NULL, NULL, TRUE, 0,
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
160 NULL, NULL, &si, &pi) == 0)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
161 {
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
162 _ftprintf (stderr, _T("Error %d launching `%s'\n"),
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
163 GetLastError (), command);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
164 return 2;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
165 }
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
166
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
167 CloseHandle (pi.hThread);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
168
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
169 /* Start pump in each I-connector */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
170 start_pump (&I_in);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
171 start_pump (&I_out);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
172 start_pump (&I_err);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
173
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
174 /* Wait for the process to complete */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
175 WaitForSingleObject (pi.hProcess, INFINITE);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
176 GetExitCodeProcess (pi.hProcess, &exit_code);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
177 CloseHandle (pi.hProcess);
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
178
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
179 /* Make pump threads eventually die out. Looks rude, I agree */
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
180 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
181 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
182 CloseHandle (GetStdHandle (STD_ERROR_HANDLE));
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
183
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
184 return exit_code;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents:
diff changeset
185 }