53 assert(_argc == (
int)_args.size());
55 if (_argv !=
nullptr) {
56 for (
int i = 0; i < _argc; ++i) {
57 PANDA_FREE_ARRAY(_argv[i]);
59 PANDA_FREE_ARRAY(_argv);
72 set_command_line(
const string &command_line) {
75 const char *p = command_line.c_str();
77 parse_unquoted_arg(p);
80 while (*p !=
'\0' && isspace(*p)) {
85 assert(_argc == 0 && _argv ==
nullptr);
86 _argc = (int)_args.size();
87 _argv = (
char **)PANDA_MALLOC_ARRAY(_argc *
sizeof(
char *));
88 for (
int i = 0; i < _argc; ++i) {
89 const string &arg = _args[i];
90 char *astr = (
char *)PANDA_MALLOC_ARRAY(arg.size() + 1);
91 memcpy(astr, arg.data(), arg.size());
92 astr[arg.size()] =
'\0';
101 void Win32ArgParser::
102 set_command_line(
const std::wstring &command_line) {
106 set_command_line(encoder.
get_text());
113 void Win32ArgParser::
114 set_system_command_line() {
115 LPWSTR command_line = GetCommandLineW();
116 set_command_line(command_line);
126 char **Win32ArgParser::
144 bool Win32ArgParser::
150 if (!envvar.empty()) {
151 std::istringstream strm(envvar);
169 HANDLE toolhelp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
171 PROCESSENTRY32 entry;
172 memset(&entry, 0,
sizeof(entry));
173 entry.dwSize =
sizeof(entry);
175 DWORD current_id = GetCurrentProcessId();
176 DWORD parent_id = -1;
178 if (Process32First(toolhelp, &entry)) {
180 if (entry.th32ProcessID == current_id) {
181 parent_id = entry.th32ParentProcessID;
184 }
while (Process32Next(toolhelp, &entry));
188 if (parent_id != -1) {
191 if (Process32First(toolhelp, &entry)) {
193 if (entry.th32ProcessID == parent_id) {
197 }
while (Process32Next(toolhelp, &entry));
201 CloseHandle(toolhelp);
203 if (basename ==
"cmd.exe") {
214 string Win32ArgParser::
215 parse_quoted_arg(
const char *&p) {
220 while (*p !=
'\0' && *p != quote) {
235 for (
int i = 0; i < num_slashes; i += 2) {
240 if ((num_slashes & 1) == 0) {
252 for (
int i = 0; i < num_slashes; ++i) {
275 void Win32ArgParser::
276 parse_unquoted_arg(
const char *&p) {
278 bool contains_quotes =
false;
279 while (*p !=
'\0' && !isspace(*p)) {
282 contains_quotes =
true;
283 result += parse_quoted_arg(p);
293 if (!contains_quotes && glob.has_glob_characters()) {
297 vector_string expand;
298 if (glob.match_files(expand) != 0) {
300 vector_string::const_iterator ei;
301 for (ei = expand.begin(); ei != expand.end(); ++ei) {
322 void Win32ArgParser::
323 save_arg(
const string &arg) {
324 _args.push_back(arg);