-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandArguments.cpp
More file actions
392 lines (357 loc) · 9.13 KB
/
CommandArguments.cpp
File metadata and controls
392 lines (357 loc) · 9.13 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright © 2026 CCP ehf.
#include "StdAfx.h"
#include "CommandArguments.h"
#include <fstream>
#ifdef _WIN32
#include <shellapi.h>
#endif
namespace
{
void Usage()
{
fprintf(stderr, "Usage: exefile [flag ...]\n");
fprintf(stderr, "flags: /?, /help: this text\n");
fprintf(stderr, " /root=<root path>\n");
fprintf(stderr, " /bin=<bin path>\n");
fprintf(stderr, " /lib=<bin path>\n");
fprintf(stderr, " /verbose , /noverbose\n");
fprintf(stderr, " /console , /noconsole, /inherit\n");
fprintf(stderr, " /aflock=<aff>\n");
fprintf(stderr, " /jessica\n");
fprintf(stderr, " /pyoptimize=<opt>\n");
fprintf(stderr, " /stderr=<path with %%p as pid>\n");
fprintf(stderr, " /stdout=<path with %%p as pid>\n");
fprintf(stderr, " /noCrashReportUpload\n");
fprintf(stderr, " /service\n");
fprintf(stderr, " /buildflavor=<flavor>\n");
fprintf(stderr, " paths can start with $(cwd)\n");
exit( 0 );
}
#ifdef _WIN32
CommandLine SplitCommandLine(const wchar_t *line)
{
int nArgs;
LPWSTR *argv = CommandLineToArgvW(line, &nArgs);
std::vector<std::wstring> res;
for(int i = 0; i<nArgs; i++)
{
res.push_back(argv[i]);
}
LocalFree(argv);
return res;
}
bool GetEnvironmentVar(std::wstring *val, const wchar_t *name)
{
wchar_t f;
DWORD size = GetEnvironmentVariableW(name, &f, 0);
if (!size)
{
return false; // not set
}
if (!val)
{
return true; //just test for existance
}
std::vector<wchar_t> buf(size);
DWORD size2 = GetEnvironmentVariableW(name, &buf[0], size);
if(size2 > 0 && size2+1 <= size)
{
*val = &buf[0];
return true;
}
return false;
}
#else
CommandLine SplitCommandLine(const wchar_t *line)
{
CommandLine result;
std::wstring arg;
bool inQuotes = false;
for( auto i = line; *i; ++i )
{
if( !inQuotes && ( *i == L' ' || *i == L'\t' ) )
{
if( !arg.empty() )
{
result.push_back( arg );
arg = L"";
continue;
}
}
else if( *i == L'"' )
{
if( !arg.empty() && arg.back() == L'\\' )
{
arg.back() = *i;
}
else
{
inQuotes = !inQuotes;
}
continue;
}
else if( *i == L'\\' )
{
if( !arg.empty() && arg.back() == L'\\' )
{
continue;
}
}
arg += *i;
}
if( !arg.empty() )
{
result.push_back( arg );
}
return result;
}
bool GetEnvironmentVar(std::wstring *val, const wchar_t *name)
{
auto env = getenv( CW2A( name ) );
if( !env )
{
return false;
}
if( val )
{
*val = CA2W( env );
}
return true;
}
#endif
std::wstring GetEnvironmentArgs()
{
std::wstring result;
GetEnvironmentVar( &result, L"EXEFILE_ARGS" );
return result;
}
std::vector<std::wstring> ExpandCommandLineWithEnvironment( const CommandLine& arguments )
{
//construct vector of arguments
auto tmpArgv = arguments;
if( tmpArgv.size() < 2 )
{
//use env var if nothing on the cmd line
std::wstring envargs = GetEnvironmentArgs();
if( envargs.size() )
{
// don't call SplitCommandLine with an empty envargs since
// it will cause the executable path to be used instead
std::vector<std::wstring> tmp = SplitCommandLine( envargs.c_str() );
tmpArgv.insert( tmpArgv.end(), tmp.begin(), tmp.end() );
}
}
return tmpArgv;
}
std::wstring TrimLeft( const std::wstring& str )
{
int start = 0;
for( std::wstring::const_iterator i = str.begin(); i != str.end(); ++i)
{
if( *i != L' ' && *i != L'\t' )
break;
++start;
}
return str.substr( start );
}
void ExpandFileContents( const std::wstring& filename, std::vector<std::wstring>& argv )
{
std::ifstream is;
#ifdef _WIN32
is.open( filename.c_str() );
#else
is.open( CW2A( filename.c_str() ) );
#endif
if( !is.good() )
{
CCP_LOGERR( "File name '%S' provided as startup argument to executable cannot be read", filename.c_str() );
return;
}
while( is.good() )
{
std::string a;
is >> a;
std::wstring wa = TrimLeft( std::wstring( CA2W( a.c_str() ) ) );
if( wa.length() > 0 )
{
if( wa[0] == L'@' )
{
std::wstring filename = wa.substr(1);
ExpandFileContents( filename, argv );
}
else
{
argv.push_back( wa );
}
}
}
}
CommandLine ExpandCommandLineWithFiles( const std::vector<std::wstring>& source )
{
CommandLine destination;
for( auto it = std::begin( source ); it != std::end( source ); ++it )
{
if( ( *it )[0] == L'@' )
{
std::wstring filename = it->substr(1);
ExpandFileContents( filename, destination );
}
else
{
destination.push_back( *it );
}
}
return destination;
}
std::wstring ToLower( const std::wstring &s )
{
std::wstring r = s;
for( size_t i = 0; i< s.size(); i++ )
{
wchar_t c = r[i];
if( iswupper( c ) )
{
wchar_t lc = towlower( c );
if( iswlower( lc ) )
{
c = lc;
}
}
r[i] = c;
}
return r;
}
}
#if _WIN32
CommandLine ParseCommandLine()
{
auto rawCommandLine = SplitCommandLine( GetCommandLineW() );
return ExpandCommandLineWithFiles( ExpandCommandLineWithEnvironment( rawCommandLine ) );
}
#elif __APPLE__
CommandLine ParseCommandLine( int argc, char* argv[] )
{
CommandLine rawCommandLine;
for( int i = 0; i < argc; ++i )
{
rawCommandLine.push_back( std::wstring( CA2W( argv[i] ) ) );
}
return ExpandCommandLineWithFiles( ExpandCommandLineWithEnvironment( rawCommandLine ) );
}
#endif
#if !_WIN32
int _wtoi( const wchar_t* str )
{
return atoi( CW2A( str ) );
}
#endif
void DumpCommandLineToDebugger( const CommandLine& commandLine )
{
#if __APPLE__
if ( !CcpIsDebuggerPresent() ) {
return;
}
#endif
for( auto it = std::begin( commandLine ); it != std::end( commandLine ); ++it )
{
#if _WIN32
OutputDebugStringW( it->c_str() );
OutputDebugStringW( L"\n" );
#elif __APPLE__
wprintf(L"%ls\n", it->c_str());
#else
#error "Unsupported platform!"
#endif
}
}
CommandArguments GetCommandArguments( const CommandLine& commandLine )
{
bool verbose = false;
CommandArguments commandArguments;
//Environment var way of turning on our spiffy new _inherit mode.
//This is used by ExeFile.com
if(GetEnvironmentVar( 0, L"EXEFILE_INHERIT" ) )
{
commandArguments.consoleMode = console_mode_inherit;
}
// Special case: If started witout arguments, it behaves as though eve.exe had
// started it. This is to support "pinning to taskbar" on windows 7.
// Later we want better taskbar support.
if( commandLine.size() < 2 )
{
commandArguments.consoleMode = console_mode_off;
}
std::wstring redirect_stderr;
std::wstring redirect_stdout;
// Old-style path for rootpath
std::wstring rootPath;
// Old-style path for binpath
std::wstring binPath;
// Old-style path for libpath
std::wstring libPath;
for( size_t i = 1; i < commandLine.size(); i++ )
{
const std::wstring &arg = commandLine[i];
const std::wstring larg = ToLower(arg);
if (larg.find(L"/?")==0 || larg.find(L"help")==0) {
Usage();
} else if (larg.find(L"/root=")==0) {
rootPath = arg.substr(6);
std::wstring path = L"root=";
path += rootPath;
commandArguments.searchPaths.push_back( path );
} else if (larg.find(L"/bin=")==0) {
binPath = arg.substr(5);
std::wstring path = L"bin=";
path += binPath;
commandArguments.searchPaths.push_back( path );
} else if (larg.find(L"/lib=")==0) {
libPath = arg.substr(5);
std::wstring path = L"lib=";
path += libPath;
commandArguments.searchPaths.push_back( path );
} else if (larg.find(L"/path:")==0 ) {
// Gather up search paths - push them to BeOS once we've loaded Blue
std::wstring path = arg.substr(6);
commandArguments.searchPaths.push_back( path );
} else if (larg == L"/verbose") {
verbose = true;
} else if (larg == L"/noverbose") {
verbose = false;
} else if (larg == L"/console") {
commandArguments.consoleMode = console_mode_create;
} else if (larg == L"/noconsole") {
commandArguments.consoleMode = console_mode_off;
} else if (larg == L"/inherit") {
commandArguments.consoleMode = console_mode_inherit;
} else if (larg == L"/nocrashreportupload") {
commandArguments.uploadMinidump = false;
} else if (larg.find(L"/aflock=")==0) {
commandArguments.affinity = _wtoi(arg.substr(8).c_str());
} else if (larg.find(L"/pyoptimize=")==0) {
commandArguments.pyOptimize = _wtoi(arg.substr(12).c_str());
} else if (larg.find(L"/stderr=") == 0) {
commandArguments.redirectStdErr = arg.substr(8);
} else if (larg.find(L"/stdout=") == 0) {
commandArguments.redirectStdOut = arg.substr(8);
} else if (larg == L"/service") {
commandArguments.asService = true;
} else if (larg.find(L"/buildflavor=") == 0) {
commandArguments.buildFlavor = arg.substr(13);
}
}
if( verbose )
{
fprintf(stdout, "%S starting up\n", commandLine.size()>0?commandLine[0].c_str() : L"ExeFile.exe");
fprintf(stdout, "/root=%S\n", rootPath.c_str());
fprintf(stdout, "/bin=%S\n", binPath.c_str());
fprintf(stdout, "/lib=%S\n", libPath.c_str());
fprintf(stdout, "/%sverbose\n", verbose?"":"no");
fprintf(stdout, "/console_mode %d\n", commandArguments.consoleMode);
fprintf(stdout, "/aflock=%d\n", commandArguments.affinity);
fprintf(stdout, "/pyoptimize=%d\n", commandArguments.pyOptimize);
fprintf(stdout, "/service=%s\n", commandArguments.asService ? "yes" : "no");
fprintf(stdout, "/buildflavor=%S\n", commandArguments.buildFlavor.c_str());
}
return commandArguments;
}