forked from adoxa/errout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrout.c
321 lines (285 loc) · 8.51 KB
/
errout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
errout.c - Send stderr to stdout.
Jason Hood, 29 June to 2 July, 2011.
Set up a WriteFile hook to redirect both stdout & stderr, maintaining the
order as you'd see it on the console (which "2>&1" doesn't do).
*/
#define PVERS L"1.10"
#define PDATE L"14 November, 2013"
#define DO_IMPORT
#include "errout.h"
#include <locale.h>
#ifdef __MINGW32__
// I think it's a flaw in gcc that it doesn't read object files from its library
// directory, hence doing this rather than adding CRT_noglob.o. Only necessary
// for MinGW32, TDM apparently doesn't do it by default (besides which, it uses
// _dowildcard, same as VC).
int _CRT_glob = 0;
#endif
#ifdef _WIN64
# define BITS L"64"
# define APPS L"Windows"
#else
# define BITS L"32"
# define APPS L"Win32"
#endif
__declspec(dllimport)
Globals global;
void help( void );
LPWSTR skip_spaces( LPWSTR );
void get_arg( LPWSTR, LPWSTR*, LPWSTR* );
DWORD CtrlHandler( DWORD event )
{
return (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT);
}
int main( void )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
HANDLE hStdErr;
FILE* stdcon;
LPWSTR argv, arg, cmd;
char cp[8];
WCHAR env[4];
BOOL delay;
DWORD rc;
rc = 0;
argv = GetCommandLine();
arg = malloc( wcslen( argv ) * sizeof(WCHAR) );
get_arg( arg, &argv, &cmd ); // skip the program name
get_arg( arg, &argv, &cmd );
if (*arg == '\0' ||
wcscmp( arg, L"/?" ) == 0 ||
wcscmp( arg, L"--help" ) == 0)
{
help();
free( arg );
return rc;
}
if (wcscmp( arg, L"--version" ) == 0)
{
_putws( L"Errout (" BITS L"-bit) version " PVERS L" (" PDATE L")." );
free( arg );
return rc;
}
#ifdef _WIN64
if (*arg == '-' && arg[1] == 'P')
{
swscanf( arg + 2, L"%u:%u", &pi.dwProcessId, &pi.dwThreadId );
pi.hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId);
pi.hThread = OpenThread( THREAD_ALL_ACCESS, FALSE, pi.dwThreadId );
InjectDLL64( &pi );
CloseHandle( pi.hThread );
CloseHandle( pi.hProcess );
return 0;
}
#endif
// Using "" for setlocale uses the system ANSI code page.
sprintf( cp, ".%u", GetConsoleOutputCP() );
setlocale( LC_CTYPE, cp );
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
global.hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
hStdErr = GetStdHandle( STD_ERROR_HANDLE );
// If a pipe creates a new console (like TDE does), then CONOUT$ will (may?)
// be created in that console, so use stderr (if it's not been redirected for
// some reason) to keep -c in the original console.
if (GetFileType( hStdErr ) == FILE_TYPE_CHAR)
{
// Can't just copy the handle, as we need to distinguish them.
DuplicateHandle( GetCurrentProcess(), hStdErr,
GetCurrentProcess(), &global.hStdCon,
0, TRUE, DUPLICATE_SAME_ACCESS );
}
else
{
global.hStdCon = CreateFile( L"CONOUT$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&sa, OPEN_EXISTING, 0, 0 );
}
// Unfortunately, the above doesn't change stdout, so create a console file.
stdcon = _wfopen( L"con", L"w" );
*env = '\0';
GetEnvironmentVariable( L"ERROUTCOL", env, lenof(env) );
if (*env != '\0')
{
LPWSTR end;
long attr = wcstol( env, &end, 16 );
if (end != env && *end == '\0' && attr < 256)
global.errcol = (WORD)attr;
}
rc = -1;
delay = FALSE;
while (*arg == '-')
{
if (arg[1] == 'a')
{
LPWSTR end;
long attr = wcstol( arg+2, &end, 16 );
if (end == arg+2 || *end != '\0' || attr >= 256)
{
fwprintf( stdcon, L"Errout: Expecting one or two hex digits: \"%s\".\n", arg );
goto tidy_up;
}
global.errcol = (WORD)attr;
}
else if (arg[1] == 'c')
{
if (arg[2] == '\0')
global.console = 3;
else
{
if (arg[2] == 'e')
global.console = 2;
else if (arg[2] == 'o')
global.console = 1;
if (global.console == 0 || arg[3] != '\0')
{
fwprintf( stdcon, L"Errout: Expecting -c[e|o]: \"%s\".\n", arg );
goto tidy_up;
}
}
}
else if (arg[1] == 'd')
{
if (arg[2] != '\0')
{
fwprintf( stdcon, L"Errout: Not expecting anything: \"%s\".\n", arg );
goto tidy_up;
}
delay = TRUE;
}
else
{
HANDLE* pFile;
BOOL append = !(arg[1] & 0x20);
switch (arg[1] | 0x20)
{
default:
fwprintf( stdcon, L"Errout: Unknown option: \"%s\".\n", arg );
goto tidy_up;
case 'o': pFile = &global.hFilOut; break;
case 'e': pFile = &global.hFilErr; break;
case 'f': pFile = &global.hFilCon; break;
}
get_arg( arg, &argv, &cmd );
*pFile = CreateFile( arg, GENERIC_WRITE, FILE_SHARE_READ, &sa,
(append) ? OPEN_ALWAYS : CREATE_ALWAYS, 0, 0 );
if (*pFile == INVALID_HANDLE_VALUE)
{
fwprintf( stdcon, L"Errout: Unable to %s \"%s\".\n",
(append) ? L"open" : L"create", arg );
goto tidy_up;
}
if (append)
SetFilePointer( *pFile, 0, NULL, FILE_END );
}
get_arg( arg, &argv, &cmd );
}
// Ignore the color if stdout is redirected and -c hasn't been used - this
// prevents a pipe writing to console from being inadvertently coloured.
if (GetFileType( global.hStdOut ) != FILE_TYPE_CHAR && global.console == 0)
global.errcol = -1;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
if (!delay)
SetStdHandle( STD_OUTPUT_HANDLE, global.hStdCon );
if (CreateProcess( NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi ))
{
// Point stdout to the new console handle. This is necessary to force the
// combination of out & err, otherwise they still remain separate. I think
// this is due to the differing buffering characteristics of character &
// file types. I couldn't find a function to change that, hence the hook.
// I originally did this earlier, but the whole reason I created this
// program was so I could pipe a 7z archive and see the file names along
// with the contents. Of course, that was the last thing I tested and it
// didn't work - "I won't extract data and program's messages to the same
// terminal." Well, now you will. :)
if (delay)
SetStdHandle( STD_OUTPUT_HANDLE, global.hStdCon );
CloseHandle( pi.hThread );
SetConsoleCtrlHandler( (PHANDLER_ROUTINE)CtrlHandler, TRUE );
WaitForSingleObject( pi.hProcess, INFINITE );
// get exit code
GetExitCodeProcess( pi.hProcess, &rc );
CloseHandle( pi.hProcess );
}
else
{
get_arg( arg, &cmd, &cmd );
fwprintf( stdcon, L"Errout: \"%s\" could not be executed.\n", arg );
rc = -2;
}
tidy_up:
fclose( stdcon );
if (global.hFilOut != NULL)
CloseHandle( global.hFilOut );
if (global.hFilErr != NULL)
CloseHandle( global.hFilErr );
if (global.hFilCon != NULL)
CloseHandle( global.hFilCon );
SetStdHandle( STD_OUTPUT_HANDLE, global.hStdOut );
CloseHandle( global.hStdCon );
free( arg );
return rc;
}
// Return the first non-space character from arg.
LPWSTR skip_spaces( LPWSTR arg )
{
while (*arg == ' ' || *arg == '\t')
++arg;
return arg;
}
// Retrieve an argument from the command line. cmd gets the existing argv; argv
// is ready for the next argument.
void get_arg( LPWSTR arg, LPWSTR* argv, LPWSTR* cmd )
{
LPWSTR line = skip_spaces( *argv );
*cmd = line;
while (*line != ' ' && *line != '\t' && *line != '\0')
{
if (*line == '"')
{
while (*++line != '"' && *line != '\0')
*arg++ = *line;
if (*line != '\0')
++line;
}
else
{
*arg++ = *line++;
}
}
if (*line != '\0')
++line;
*arg = '\0';
*argv = line;
}
void help( void )
{
_putws(
L"Errout by Jason Hood <jadoxa@yahoo.com.au>.\n"
L"Version " PVERS L" (" PDATE L"). Freeware.\n"
L"http://errout.adoxa.vze.com/\n"
L"\n"
L"Send standard error (stderr) to standard output (stdout) in " APPS L" console\n"
L"programs, and optionally to the console and/or files as well.\n"
L"\n"
L"errout [options] program [args]\n"
L"\n"
L" -a[B]F\tset stderr to foreground F & background B (0 if absent)\n"
L" -c[e|o]\talso output both, or just stderr/stdout, to the console\n"
L" -d\t\tdelay the hook till after program is started\n"
L" -e FILE\talso write stderr to FILE\n"
L" -f FILE\talso write both to FILE\n"
L" -o FILE\talso write stdout to FILE\n"
L" program\trun the specified program with its arguments\n"
L"\n"
L"Lower case letter will create FILE; upper case will append.\n"
L"F & B are hex digits; see COLOR/? for the values.\n"
L"Set the environment variable ERROUTCOL=[B]F to use -a by default.\n"
L"Delay may be useful for some programs (notably 7z)."
);
}