-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
CommandLine.cpp
303 lines (257 loc) · 8.84 KB
/
CommandLine.cpp
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
//===--- CommandLine.cpp - OS-specific command line arguments -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// OS-specific command line argument handling is defined here.
//
//===----------------------------------------------------------------------===//
#include <cassert>
#include <climits>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include "swift/Runtime/Debug.h"
#include "../SwiftShims/GlobalObjects.h"
#include "../SwiftShims/RuntimeStubs.h"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <shellapi.h>
#endif
// Backing storage for overrides of `Swift.CommandLine.arguments`.
static char **_swift_stdlib_ProcessOverrideUnsafeArgv = nullptr;
static int _swift_stdlib_ProcessOverrideUnsafeArgc = 0;
SWIFT_RUNTIME_STDLIB_API
void _swift_stdlib_overrideUnsafeArgvArgc(char **argv, int argc) {
_swift_stdlib_ProcessOverrideUnsafeArgv = argv;
_swift_stdlib_ProcessOverrideUnsafeArgc = argc;
}
#if defined(__APPLE__)
// NOTE: forward declare this rather than including crt_externs.h as not all
// SDKs provide it
extern "C" char ***_NSGetArgv(void);
extern "C" int *_NSGetArgc(void);
SWIFT_RUNTIME_STDLIB_API
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
return _swift_stdlib_ProcessOverrideUnsafeArgv;
}
*outArgLen = *_NSGetArgc();
return *_NSGetArgv();
}
#elif defined(__linux__) || defined(__CYGWIN__)
SWIFT_RUNTIME_STDLIB_API
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
return _swift_stdlib_ProcessOverrideUnsafeArgv;
}
FILE *cmdline = fopen("/proc/self/cmdline", "rb");
if (!cmdline) {
swift::fatalError(
0, "Fatal error: Unable to open interface to '/proc/self/cmdline'.\n");
}
char *arg = nullptr;
size_t size = 0;
std::vector<char *> argvec;
while (getdelim(&arg, &size, 0, cmdline) != -1) {
argvec.push_back(strdup(arg));
}
if (arg) {
free(arg);
}
fclose(cmdline);
*outArgLen = argvec.size();
auto outBuf = static_cast<char **>(calloc(argvec.size() + 1, sizeof(char *)));
std::copy(argvec.begin(), argvec.end(), outBuf);
outBuf[argvec.size()] = nullptr;
return outBuf;
}
#elif defined(_WIN32)
#include <stdlib.h>
SWIFT_RUNTIME_STDLIB_API
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
return _swift_stdlib_ProcessOverrideUnsafeArgv;
}
*outArgLen = 0;
LPWSTR *szArgList;
int nArgs;
szArgList = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if (szArgList == nullptr)
return nullptr;
std::vector<char *> argv;
for (int i = 0; i < nArgs; ++i) {
int szBufferSize =
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, szArgList[i], -1,
nullptr, 0, nullptr, nullptr);
if (szBufferSize == 0) {
swift::fatalError(0,
"Fatal error: Could not retrieve commandline "
"arguments: %u\n",
GetLastError());
return nullptr;
}
char *buffer = static_cast<char *>(
calloc(static_cast<size_t>(szBufferSize), sizeof(char)));
if (buffer == nullptr) {
swift::fatalError(0,
"Fatal error: Could not allocate space for commandline"
"arguments");
return nullptr;
}
if (!WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, szArgList[i], -1,
buffer, szBufferSize, nullptr, nullptr)) {
swift::fatalError(0,
"Fatal error: Conversion to UTF-8 failed for "
"commandline arguments");
return nullptr;
}
argv.push_back(buffer);
}
LocalFree(szArgList);
char **args = static_cast<char **>(calloc(argv.size() + 1, sizeof(char *)));
std::copy(argv.begin(), argv.end(), args);
args[argv.size()] = nullptr;
assert(argv.size() < INT_MAX && "overflow");
*outArgLen = static_cast<int>(argv.size());
return args;
}
#elif defined(__FreeBSD__)
#include <errno.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <unistd.h>
SWIFT_RUNTIME_STDLIB_API
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
return _swift_stdlib_ProcessOverrideUnsafeArgv;
}
char *argPtr = nullptr; // or use ARG_MAX? 8192 is used in LLDB though..
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, getpid()};
size_t argPtrSize = 0;
for (int i = 0; i < 3 && !argPtr; ++i) { // give up after 3 tries
if (sysctl(mib, 4, nullptr, &argPtrSize, nullptr, 0) != -1) {
argPtr = static_cast<char *>(malloc(argPtrSize));
if (sysctl(mib, 4, argPtr, &argPtrSize, nullptr, 0) == -1) {
free(argPtr);
argPtr = nullptr;
if (errno != ENOMEM)
break;
}
} else {
break;
}
}
if (!argPtr)
swift::fatalError(0,
"Fatal error: Could not retrieve commandline "
"arguments: sysctl: %s.\n",
strerror(errno));
char *curPtr = argPtr;
char *endPtr = argPtr + argPtrSize;
std::vector<char *> argvec;
for (; curPtr < endPtr; curPtr += strlen(curPtr) + 1)
argvec.push_back(strdup(curPtr));
*outArgLen = argvec.size();
auto outBuf = static_cast<char **>(calloc(argvec.size() + 1, sizeof(char *)));
std::copy(argvec.begin(), argvec.end(), outBuf);
outBuf[argvec.size()] = nullptr;
free(argPtr);
return outBuf;
}
#elif defined(__wasi__)
#include <stdlib.h>
#include <wasi/api.h>
#include <wasi/libc.h>
SWIFT_RUNTIME_STDLIB_API
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
return _swift_stdlib_ProcessOverrideUnsafeArgv;
}
__wasi_errno_t err;
size_t argv_buf_size = 0;
size_t argc = 0;
err = __wasi_args_sizes_get(&argc, &argv_buf_size);
if (err != __WASI_ERRNO_SUCCESS) {
swift::fatalError(0,
"Fatal error: Could not retrieve commandline "
"arguments: %d.\n", static_cast<int>(err));
return nullptr;
}
char *argv_buf = static_cast<char *>(malloc(argv_buf_size));
char **argv = static_cast<char **>(calloc(argc + 1, sizeof(char *)));
err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf);
if (err != __WASI_ERRNO_SUCCESS) {
swift::fatalError(0,
"Fatal error: Could not retrieve commandline "
"arguments: %d.\n", static_cast<int>(err));
return nullptr;
}
*outArgLen = static_cast<int>(argc);
return argv;
}
#elif defined(__OpenBSD__)
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/exec.h>
SWIFT_RUNTIME_STDLIB_API
char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
return _swift_stdlib_ProcessOverrideUnsafeArgv;
}
int mib[2] = {CTL_VM, VM_PSSTRINGS};
struct _ps_strings _ps;
size_t len = sizeof(_ps);
if (sysctl(mib, 2, &_ps, &len, NULL, 0) == -1) {
char **empty_argv = static_cast<char **>(calloc(1, sizeof(char *)));
empty_argv[0] = nullptr;
*outArgLen = 0;
return empty_argv;
}
struct ps_strings *ps = static_cast<struct ps_strings *>(_ps.val);
*outArgLen = ps->ps_nargvstr;
char **argv_copy =
static_cast<char **>(calloc(ps->ps_nargvstr + 1, sizeof(char *)));
for(int i = 0; i < ps->ps_nargvstr; i++) {
argv_copy[i] = strdup(ps->ps_argvstr[i]);
}
argv_copy[ps->ps_nargvstr] = nullptr;
return argv_copy;
}
#else // Add your favorite OS's command line arg grabber here.
SWIFT_RUNTIME_STDLIB_API
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
return _swift_stdlib_ProcessOverrideUnsafeArgv;
}
swift::fatalError(
0,
"Fatal error: Command line arguments not supported on this platform.\n");
}
#endif