| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : procutils.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#ifdef __FreeBSD__ |
| 26 |
#include <fcntl.h> |
| 27 |
#include <kvm.h> |
| 28 |
#include <paths.h> |
| 29 |
#include <sys/param.h> |
| 30 |
#include <sys/sysctl.h> |
| 31 |
#include <sys/user.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "asyncprocess.h" |
| 35 |
#include "cl_command_event.h" |
| 36 |
#include "codelite_events.h" |
| 37 |
#include "file_logger.h" |
| 38 |
#include "fileutils.h" |
| 39 |
#include "processreaderthread.h" |
| 40 |
#include "procutils.h" |
| 41 |
#include "winprocess.h" |
| 42 |
#include "wx/tokenzr.h" |
| 43 |
|
| 44 |
#include <memory> |
| 45 |
#include <stdio.h> |
| 46 |
#ifdef __WXMSW__ |
| 47 |
#include "wx/msw/private.h" |
| 48 |
#include "wx/textbuf.h" |
| 49 |
#ifndef pclose |
| 50 |
#define pclose _pclose |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifndef popen |
| 54 |
#define popen _popen |
| 55 |
#endif |
| 56 |
|
| 57 |
#endif |
| 58 |
|
| 59 |
//------------------------------------------------------------------- |
| 60 |
// clShellProcessEvent |
| 61 |
//------------------------------------------------------------------- |
| 62 |
wxDEFINE_EVENT(wxEVT_SHELL_ASYNC_PROCESS_TERMINATED, clShellProcessEvent); |
| 63 |
clShellProcessEvent::clShellProcessEvent(const clShellProcessEvent& event) { *this = event; } |
| 64 |
|
| 65 |
clShellProcessEvent::clShellProcessEvent(wxEventType commandType, int winid) |
| 66 |
: clCommandEvent(commandType, winid) |
| 67 |
{ |
| 68 |
} |
| 69 |
|
| 70 |
clShellProcessEvent::~clShellProcessEvent() {} |
| 71 |
|
| 72 |
clShellProcessEvent& clShellProcessEvent::operator=(const clShellProcessEvent& src) |
| 73 |
{ |
| 74 |
clCommandEvent::operator=(src); |
| 75 |
m_pid = src.m_pid; |
| 76 |
m_exitCode = src.m_exitCode; |
| 77 |
m_output = src.m_output; |
| 78 |
return *this; |
| 79 |
} |
| 80 |
|
| 81 |
//------------------------------------------------------------------- |
| 82 |
// clShellProcessEvent |
| 83 |
//------------------------------------------------------------------- |
| 84 |
|
| 85 |
ProcUtils::ProcUtils() {} |
| 86 |
|
| 87 |
ProcUtils::~ProcUtils() {} |
| 88 |
|
| 89 |
void ProcUtils::GetProcTree(std::map<unsigned long, bool>& parentsMap, long pid) |
| 90 |
{ |
| 91 |
#ifdef __WXMSW__ |
| 92 |
OSVERSIONINFO osver; |
| 93 |
|
| 94 |
// Check to see if were running under Windows95 or |
| 95 |
// Windows NT. |
| 96 |
osver.dwOSVersionInfoSize = sizeof(osver); |
| 97 |
if(!GetVersionEx(&osver)) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
// get child processes of this node |
| 106 |
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 107 |
if(!hProcessSnap) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// Fill in the size of the structure before using it. |
| 112 |
PROCESSENTRY32 pe; |
| 113 |
memset(&pe, 0, sizeof(pe)); |
| 114 |
pe.dwSize = sizeof(PROCESSENTRY32); |
| 115 |
|
| 116 |
// Walk the snapshot of the processes, and for each process, |
| 117 |
// kill it if its parent is pid. |
| 118 |
if(!Process32First(hProcessSnap, &pe)) { |
| 119 |
// Can't get first process. |
| 120 |
CloseHandle(hProcessSnap); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
parentsMap[pid] = true; |
| 125 |
|
| 126 |
do { |
| 127 |
if(parentsMap.find(pe.th32ParentProcessID) != parentsMap.end()) { |
| 128 |
// get the process handle |
| 129 |
HANDLE hProcess = ::OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, |
| 130 |
FALSE, // not inheritable |
| 131 |
(DWORD)pid); |
| 132 |
if(hProcess != NULL) { |
| 133 |
CloseHandle(hProcess); |
| 134 |
// dont kill the process, just keep its ID |
| 135 |
parentsMap[pe.th32ProcessID] = true; |
| 136 |
} |
| 137 |
} |
| 138 |
} while(Process32Next(hProcessSnap, &pe)); |
| 139 |
CloseHandle(hProcessSnap); |
| 140 |
#else |
| 141 |
parentsMap[pid] = true; |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
PidVec_t ProcUtils::PS(const wxString& name) |
| 146 |
{ |
| 147 |
PidVec_t V; |
| 148 |
wxString command; |
| 149 |
size_t IMGNAME_COL = 0; |
| 150 |
size_t PID_COL = 0; |
| 151 |
size_t MIN_COLUMNS_NUMBER = 0; |
| 152 |
|
| 153 |
#ifdef __WXMSW__ |
| 154 |
command << "tasklist"; |
| 155 |
IMGNAME_COL = 0; |
| 156 |
PID_COL = 1; |
| 157 |
MIN_COLUMNS_NUMBER = 2; |
| 158 |
#else |
| 159 |
command << "ps ax"; |
| 160 |
IMGNAME_COL = 4; |
| 161 |
PID_COL = 0; |
| 162 |
MIN_COLUMNS_NUMBER = 5; |
| 163 |
#endif |
| 164 |
command = WrapInShell(command); |
| 165 |
|
| 166 |
wxString processOutput; |
| 167 |
IProcess::Ptr_t p(::CreateSyncProcess(command, IProcessCreateDefault | IProcessCreateWithHiddenConsole)); |
| 168 |
if(p) { |
| 169 |
p->WaitForTerminate(processOutput); |
| 170 |
} |
| 171 |
|
| 172 |
// Search for a match |
| 173 |
|
| 174 |
// tasklist example output: |
| 175 |
// |
| 176 |
// Image Name PID Session Name Session# Mem Usage |
| 177 |
//========================= ======== ================ =========== ============ |
| 178 |
// wininit.exe 1000 Services 0 2,320 K |
| 179 |
// csrss.exe 1008 Console 1 3,860 K |
| 180 |
|
| 181 |
// ps ax example output: |
| 182 |
// |
| 183 |
// PID TTY STAT TIME COMMAND |
| 184 |
// 52 ? S 0:00 dbus-launch --autolaunch 811d5cdefd9d461891b6596cca7a6233 --binary-syntax |
| 185 |
// 2920 ? Ss 0:00 ssh-agent |
| 186 |
|
| 187 |
wxArrayString lines = ::wxStringTokenize(processOutput, "\n", wxTOKEN_STRTOK); |
| 188 |
for(wxString& line : lines) { |
| 189 |
line.Trim().Trim(false); |
| 190 |
wxArrayString parts = ::wxStringTokenize(line, " \t", wxTOKEN_STRTOK); |
| 191 |
if(parts.size() < MIN_COLUMNS_NUMBER) { |
| 192 |
continue; |
| 193 |
} |
| 194 |
wxString& imageName = parts.Item(IMGNAME_COL); |
| 195 |
wxString& pid = parts.Item(PID_COL); |
| 196 |
if(FileUtils::FuzzyMatch(name, imageName)) { |
| 197 |
long nPid = -1; |
| 198 |
if(pid.ToCLong(&nPid)) { |
| 199 |
V.push_back({ imageName, nPid }); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
return V; |
| 204 |
} |
| 205 |
|
| 206 |
wxString ProcUtils::GetProcessNameByPid(long pid) |
| 207 |
{ |
| 208 |
#ifdef __WXMSW__ |
| 209 |
// go over the process modules and get the full path of |
| 210 |
// the executeable |
| 211 |
HANDLE hModuleSnap = INVALID_HANDLE_VALUE; |
| 212 |
MODULEENTRY32 me32; |
| 213 |
|
| 214 |
// Take a snapshot of all modules in the specified process. |
| 215 |
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, (DWORD)pid); |
| 216 |
if(hModuleSnap == INVALID_HANDLE_VALUE) { |
| 217 |
return wxEmptyString; |
| 218 |
} |
| 219 |
|
| 220 |
// Set the size of the structure before using it. |
| 221 |
me32.dwSize = sizeof(MODULEENTRY32); |
| 222 |
|
| 223 |
// Retrieve information about the first module, |
| 224 |
// and exit if unsuccessful |
| 225 |
if(!Module32First(hModuleSnap, &me32)) { |
| 226 |
CloseHandle(hModuleSnap); // Must clean up the |
| 227 |
// snapshot object! |
| 228 |
return wxEmptyString; |
| 229 |
} |
| 230 |
|
| 231 |
// get the name of the process (it is located in the first entry) |
| 232 |
CloseHandle(hModuleSnap); |
| 233 |
return me32.szExePath; |
| 234 |
|
| 235 |
#elif defined(__FreeBSD__) |
| 236 |
kvm_t* kvd; |
| 237 |
struct kinfo_proc* ki; |
| 238 |
int nof_procs; |
| 239 |
wxString cmd; |
| 240 |
|
| 241 |
if(!(kvd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL))) |
| 242 |
return wxEmptyString; |
| 243 |
|
| 244 |
if(!(ki = kvm_getprocs(kvd, KERN_PROC_PID, pid, &nof_procs))) { |
| 245 |
kvm_close(kvd); |
| 246 |
return wxEmptyString; |
| 247 |
} |
| 248 |
|
| 249 |
cmd = wxString(ki->ki_ocomm, wxConvUTF8); |
| 250 |
kvm_close(kvd); |
| 251 |
|
| 252 |
return (cmd); |
| 253 |
#else |
| 254 |
wxArrayString output; |
| 255 |
ExecuteCommand(wxT("ps -A -o pid,command --no-heading"), output); |
| 256 |
// parse the output and search for our process ID |
| 257 |
for(size_t i = 0; i < output.GetCount(); i++) { |
| 258 |
wxString line = output.Item(i); |
| 259 |
// remove whitespaces |
| 260 |
line = line.Trim(); |
| 261 |
line = line.Trim(false); |
| 262 |
// get the process ID |
| 263 |
wxString spid = line.BeforeFirst(wxT(' ')); |
| 264 |
long cpid(0); |
| 265 |
spid.ToLong(&cpid); |
| 266 |
if(cpid == pid) { |
| 267 |
// we got a match, extract the command, it is in the second column |
| 268 |
wxString command = line.AfterFirst(wxT(' ')); |
| 269 |
return command; |
| 270 |
} |
| 271 |
} |
| 272 |
return wxEmptyString; // Not implemented yet |
| 273 |
#endif |
| 274 |
} |
| 275 |
|
| 276 |
void ProcUtils::ExecuteCommand(const wxString& command, wxArrayString& output, long flags) |
| 277 |
{ |
| 278 |
#ifdef __WXMSW__ |
| 279 |
wxExecute(command, output, flags); |
| 280 |
#else |
| 281 |
FILE* fp; |
| 282 |
char line[512]; |
| 283 |
memset(line, 0, sizeof(line)); |
| 284 |
fp = popen(command.mb_str(wxConvUTF8), "r"); |
| 285 |
if(fp) { |
| 286 |
while(fgets(line, sizeof(line), fp)) { |
| 287 |
output.Add(wxString(line, wxConvUTF8)); |
| 288 |
memset(line, 0, sizeof(line)); |
| 289 |
} |
| 290 |
pclose(fp); |
| 291 |
} |
| 292 |
#endif |
| 293 |
} |
| 294 |
|
| 295 |
void ProcUtils::ExecuteInteractiveCommand(const wxString& command) { wxShell(command); } |
| 296 |
|
| 297 |
void ProcUtils::GetProcessList(std::vector<ProcessEntry>& proclist) |
| 298 |
{ |
| 299 |
#ifdef __WXMSW__ |
| 300 |
OSVERSIONINFO osver; |
| 301 |
|
| 302 |
// Check to see if were running under Windows95 or |
| 303 |
// Windows NT. |
| 304 |
osver.dwOSVersionInfoSize = sizeof(osver); |
| 305 |
if(!GetVersionEx(&osver)) { |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT) { |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
// get child processes of this node |
| 314 |
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 315 |
if(!hProcessSnap) { |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
// Fill in the size of the structure before using it. |
| 320 |
PROCESSENTRY32 pe; |
| 321 |
memset(&pe, 0, sizeof(pe)); |
| 322 |
pe.dwSize = sizeof(PROCESSENTRY32); |
| 323 |
|
| 324 |
// Walk the snapshot of the processes, and for each process, |
| 325 |
// kill it if its parent is pid. |
| 326 |
if(!Process32First(hProcessSnap, &pe)) { |
| 327 |
// Can't get first process. |
| 328 |
CloseHandle(hProcessSnap); |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
do { |
| 333 |
ProcessEntry entry; |
| 334 |
entry.name = pe.szExeFile; |
| 335 |
entry.pid = (long)pe.th32ProcessID; |
| 336 |
proclist.push_back(entry); |
| 337 |
} while(Process32Next(hProcessSnap, &pe)); |
| 338 |
CloseHandle(hProcessSnap); |
| 339 |
|
| 340 |
#elif defined(__FreeBSD__) |
| 341 |
kvm_t* kvd; |
| 342 |
struct kinfo_proc* ki; |
| 343 |
int nof_procs, i; |
| 344 |
|
| 345 |
if(!(kvd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL))) |
| 346 |
return; |
| 347 |
|
| 348 |
if(!(ki = kvm_getprocs(kvd, KERN_PROC_PROC, 0, &nof_procs))) { |
| 349 |
kvm_close(kvd); |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
for(i = 0; i < nof_procs; i++) { |
| 354 |
ProcessEntry entry; |
| 355 |
entry.pid = ki[i].ki_pid; |
| 356 |
entry.name = wxString(ki[i].ki_ocomm, wxConvUTF8); |
| 357 |
proclist.push_back(entry); |
| 358 |
} |
| 359 |
kvm_close(kvd); |
| 360 |
|
| 361 |
#else |
| 362 |
// GTK and other |
| 363 |
wxArrayString output; |
| 364 |
#if defined(__WXGTK__) |
| 365 |
ExecuteCommand(wxT("ps -A -o pid,command --no-heading"), output); |
| 366 |
#elif defined(__WXMAC__) |
| 367 |
// Mac does not like the --no-heading... |
| 368 |
ExecuteCommand(wxT("ps -A -o pid,command "), output); |
| 369 |
#endif |
| 370 |
for(size_t i = 0; i < output.GetCount(); i++) { |
| 371 |
wxString line = output.Item(i); |
| 372 |
// remove whitespaces |
| 373 |
line = line.Trim().Trim(false); |
| 374 |
|
| 375 |
// get the process ID |
| 376 |
ProcessEntry entry; |
| 377 |
wxString spid = line.BeforeFirst(wxT(' ')); |
| 378 |
spid.ToLong(&entry.pid); |
| 379 |
entry.name = line.AfterFirst(wxT(' ')); |
| 380 |
|
| 381 |
if(entry.pid == 0 && i > 0) { |
| 382 |
// probably this line belongs to the provious one |
| 383 |
ProcessEntry e = proclist.back(); |
| 384 |
proclist.pop_back(); |
| 385 |
e.name << entry.name; |
| 386 |
proclist.push_back(e); |
| 387 |
} else { |
| 388 |
proclist.push_back(entry); |
| 389 |
} |
| 390 |
} |
| 391 |
#endif |
| 392 |
} |
| 393 |
|
| 394 |
void ProcUtils::GetChildren(long pid, std::vector<long>& proclist) |
| 395 |
{ |
| 396 |
#ifdef __WXMSW__ |
| 397 |
OSVERSIONINFO osver; |
| 398 |
|
| 399 |
// Check to see if were running under Windows95 or |
| 400 |
// Windows NT. |
| 401 |
osver.dwOSVersionInfoSize = sizeof(osver); |
| 402 |
if(!GetVersionEx(&osver)) { |
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
// get child processes of this node |
| 411 |
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 412 |
if(!hProcessSnap) { |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
// Fill in the size of the structure before using it. |
| 417 |
PROCESSENTRY32 pe; |
| 418 |
memset(&pe, 0, sizeof(pe)); |
| 419 |
pe.dwSize = sizeof(PROCESSENTRY32); |
| 420 |
|
| 421 |
// Walk the snapshot of the processes, and for each process, |
| 422 |
// kill it if its parent is pid. |
| 423 |
if(!Process32First(hProcessSnap, &pe)) { |
| 424 |
// Can't get first process. |
| 425 |
CloseHandle(hProcessSnap); |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
// loop over all processes and collect all the processes their parent |
| 430 |
// pid matches PID |
| 431 |
do { |
| 432 |
if((long)pe.th32ParentProcessID == pid) { |
| 433 |
proclist.push_back((long)pe.th32ProcessID); |
| 434 |
} |
| 435 |
} while(Process32Next(hProcessSnap, &pe)); |
| 436 |
CloseHandle(hProcessSnap); |
| 437 |
|
| 438 |
#elif defined(__FreeBSD__) |
| 439 |
kvm_t* kvd; |
| 440 |
struct kinfo_proc* ki; |
| 441 |
int nof_procs, i; |
| 442 |
|
| 443 |
if(!(kvd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL))) |
| 444 |
return; |
| 445 |
|
| 446 |
if(!(ki = kvm_getprocs(kvd, KERN_PROC_PROC, pid, &nof_procs))) { |
| 447 |
kvm_close(kvd); |
| 448 |
return; |
| 449 |
} |
| 450 |
|
| 451 |
for(i = 0; i < nof_procs; i++) { |
| 452 |
if(ki[i].ki_ppid == pid) |
| 453 |
proclist.push_back(ki[i].ki_pid); |
| 454 |
} |
| 455 |
|
| 456 |
kvm_close(kvd); |
| 457 |
|
| 458 |
#else |
| 459 |
// GTK and other |
| 460 |
wxArrayString output; |
| 461 |
#ifdef __WXGTK__ |
| 462 |
ExecuteCommand(wxT("ps -A -o pid,ppid --no-heading"), output); |
| 463 |
#else |
| 464 |
ExecuteCommand(wxT("ps -A -o pid,ppid "), output); |
| 465 |
#endif |
| 466 |
// parse the output and search for our process ID |
| 467 |
for(size_t i = 0; i < output.GetCount(); i++) { |
| 468 |
long lpid(0); |
| 469 |
long lppid(0); |
| 470 |
wxString line = output.Item(i); |
| 471 |
|
| 472 |
// remove whitespaces |
| 473 |
line = line.Trim().Trim(false); |
| 474 |
|
| 475 |
// get the process ID |
| 476 |
wxString spid = line.BeforeFirst(wxT(' ')); |
| 477 |
spid.ToLong(&lpid); |
| 478 |
|
| 479 |
// get the process Parent ID |
| 480 |
wxString sppid = line.AfterFirst(wxT(' ')); |
| 481 |
sppid.ToLong(&lppid); |
| 482 |
if(lppid == pid) { |
| 483 |
proclist.push_back(lpid); |
| 484 |
} |
| 485 |
} |
| 486 |
#endif |
| 487 |
} |
| 488 |
|
| 489 |
bool ProcUtils::Shell(const wxString& programConsoleCommand) |
| 490 |
{ |
| 491 |
wxString cmd; |
| 492 |
#ifdef __WXMSW__ |
| 493 |
wxChar* shell = wxGetenv(wxT("COMSPEC")); |
| 494 |
if(!shell) { |
| 495 |
shell = (wxChar*)wxT("CMD.EXE"); |
| 496 |
} |
| 497 |
|
| 498 |
// just the shell |
| 499 |
cmd = shell; |
| 500 |
#elif defined(__WXMAC__) |
| 501 |
wxString path = wxGetCwd(); |
| 502 |
cmd = wxString(wxT("osascript -e 'tell application \"Terminal\"' -e 'activate' -e 'do script with command \"cd ") + |
| 503 |
path + wxT("\"' -e 'end tell'")); |
| 504 |
#else // non-windows |
| 505 |
// try to locate the configured terminal |
| 506 |
wxString terminal; |
| 507 |
wxString where; |
| 508 |
wxArrayString tokens; |
| 509 |
wxArrayString configuredTerminal; |
| 510 |
|
| 511 |
terminal = wxT("xterm"); |
| 512 |
if(!programConsoleCommand.IsEmpty()) { |
| 513 |
tokens = wxStringTokenize(programConsoleCommand, wxT(" "), wxTOKEN_STRTOK); |
| 514 |
if(!tokens.IsEmpty()) { |
| 515 |
configuredTerminal = wxStringTokenize(tokens.Item(0), wxT("/"), wxTOKEN_STRTOK); |
| 516 |
if(!configuredTerminal.IsEmpty()) { |
| 517 |
terminal = configuredTerminal.Last(); |
| 518 |
tokens.Clear(); |
| 519 |
configuredTerminal.Clear(); |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
if(Locate(terminal, where)) { |
| 524 |
if(terminal == wxT("konsole")) { |
| 525 |
wxString path = wxGetCwd(); |
| 526 |
terminal.Clear(); |
| 527 |
terminal << where << wxT(" --workdir \"") << path << wxT("\""); |
| 528 |
} else { |
| 529 |
terminal = where; |
| 530 |
} |
| 531 |
} else { |
| 532 |
return false; |
| 533 |
} |
| 534 |
cmd = terminal; |
| 535 |
terminal.Clear(); |
| 536 |
#endif |
| 537 |
return wxExecute(cmd, wxEXEC_ASYNC) != 0; |
| 538 |
} |
| 539 |
|
| 540 |
bool ProcUtils::Locate(const wxString& name, wxString& where) |
| 541 |
{ |
| 542 |
wxString command; |
| 543 |
wxArrayString output; |
| 544 |
command << wxT("which \"") << name << wxT("\""); |
| 545 |
ProcUtils::ExecuteCommand(command, output); |
| 546 |
|
| 547 |
if(output.IsEmpty() == false) { |
| 548 |
wxString interstingLine = output.Item(0); |
| 549 |
|
| 550 |
if(interstingLine.Trim().Trim(false).IsEmpty()) { |
| 551 |
return false; |
| 552 |
} |
| 553 |
|
| 554 |
if(!interstingLine.StartsWith(wxT("which: no "))) { |
| 555 |
where = output.Item(0); |
| 556 |
where = where.Trim().Trim(false); |
| 557 |
return true; |
| 558 |
} |
| 559 |
} |
| 560 |
return false; |
| 561 |
} |
| 562 |
|
| 563 |
void ProcUtils::SafeExecuteCommand(const wxString& command, wxArrayString& output) |
| 564 |
{ |
| 565 |
#ifdef __WXMSW__ |
| 566 |
wxString errMsg; |
| 567 |
LOG_IF_TRACE { clDEBUG1() << "executing process:" << command << endl; } |
| 568 |
std::unique_ptr<WinProcess> proc{ WinProcess::Execute(command, errMsg) }; |
| 569 |
if(!proc) { |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
// wait for the process to terminate |
| 574 |
wxString tmpbuf; |
| 575 |
wxString buff; |
| 576 |
|
| 577 |
LOG_IF_TRACE { clDEBUG1() << "reading process output..." << endl; } |
| 578 |
while(proc->IsAlive()) { |
| 579 |
tmpbuf.Clear(); |
| 580 |
if(proc->Read(tmpbuf)) { |
| 581 |
// as long as we read something, dont sleep... |
| 582 |
buff << tmpbuf; |
| 583 |
} else { |
| 584 |
wxThread::Sleep(1); |
| 585 |
} |
| 586 |
} |
| 587 |
tmpbuf.Clear(); |
| 588 |
LOG_IF_TRACE |
| 589 |
{ |
| 590 |
clDEBUG1() << "process terminated" << endl; |
| 591 |
// Read any unread output |
| 592 |
clDEBUG1() << "reading process output remainder..." << endl; |
| 593 |
} |
| 594 |
proc->Read(tmpbuf); |
| 595 |
while(!tmpbuf.IsEmpty()) { |
| 596 |
buff << tmpbuf; |
| 597 |
tmpbuf.Clear(); |
| 598 |
proc->Read(tmpbuf); |
| 599 |
} |
| 600 |
proc->Cleanup(); |
| 601 |
LOG_IF_TRACE { clDEBUG1() << "reading process output remainder...done" << endl; } |
| 602 |
|
| 603 |
// Convert buff into wxArrayString |
| 604 |
output = ::wxStringTokenize(buff, "\n", wxTOKEN_STRTOK); |
| 605 |
#else |
| 606 |
ProcUtils::ExecuteCommand(command, output); |
| 607 |
#endif |
| 608 |
} |
| 609 |
|
| 610 |
wxString ProcUtils::SafeExecuteCommand(const wxString& command) |
| 611 |
{ |
| 612 |
wxString strOut; |
| 613 |
wxArrayString arr; |
| 614 |
SafeExecuteCommand(command, arr); |
| 615 |
|
| 616 |
for(size_t i = 0; i < arr.GetCount(); ++i) { |
| 617 |
strOut << arr.Item(i) << "\n"; |
| 618 |
} |
| 619 |
|
| 620 |
if(!strOut.IsEmpty()) { |
| 621 |
strOut.RemoveLast(); |
| 622 |
} |
| 623 |
return strOut; |
| 624 |
} |
| 625 |
|
| 626 |
wxString ProcUtils::GrepCommandOutput(const std::vector<wxString>& cmd, const wxString& find_what) |
| 627 |
{ |
| 628 |
IProcess::Ptr_t proc(::CreateAsyncProcess(nullptr, cmd, IProcessCreateDefault | IProcessCreateSync)); |
| 629 |
if(!proc) { |
| 630 |
return wxEmptyString; |
| 631 |
} |
| 632 |
|
| 633 |
wxString output; |
| 634 |
proc->WaitForTerminate(output); |
| 635 |
auto lines = ::wxStringTokenize(output, "\n", wxTOKEN_STRTOK); |
| 636 |
for(wxString& line : lines) { |
| 637 |
line.Trim(); |
| 638 |
if(line.Contains(find_what)) { |
| 639 |
return line; |
| 640 |
} |
| 641 |
} |
| 642 |
return wxEmptyString; |
| 643 |
} |
| 644 |
|
| 645 |
namespace |
| 646 |
{ |
| 647 |
class ProcessHelper : public wxProcess |
| 648 |
{ |
| 649 |
wxEvtHandler* m_handler = nullptr; |
| 650 |
wxString m_output_file; |
| 651 |
wxString m_process_output; |
| 652 |
|
| 653 |
private: |
| 654 |
void ReadOutput() |
| 655 |
{ |
| 656 |
FileUtils::Deleter d{ m_output_file }; |
| 657 |
FileUtils::ReadFileContent(m_output_file, m_process_output); |
| 658 |
} |
| 659 |
|
| 660 |
public: |
| 661 |
ProcessHelper(wxEvtHandler* sink, const wxString& output_file) |
| 662 |
: m_handler(sink) |
| 663 |
, m_output_file(output_file) |
| 664 |
{ |
| 665 |
} |
| 666 |
virtual ~ProcessHelper() {} |
| 667 |
|
| 668 |
// Notify about the process termination |
| 669 |
void OnTerminate(int pid, int status) override |
| 670 |
{ |
| 671 |
if(status == 0) { |
| 672 |
ReadOutput(); |
| 673 |
} |
| 674 |
|
| 675 |
clShellProcessEvent event_terminated{ wxEVT_SHELL_ASYNC_PROCESS_TERMINATED }; |
| 676 |
event_terminated.SetPid(pid); |
| 677 |
event_terminated.SetExitCode(status); |
| 678 |
event_terminated.SetOutput(m_process_output); |
| 679 |
m_handler->QueueEvent(event_terminated.Clone()); |
| 680 |
delete this; |
| 681 |
} |
| 682 |
}; |
| 683 |
} // namespace |
| 684 |
|
| 685 |
bool ProcUtils::ShellExecAsync(const wxString& command, long* pid, wxEvtHandler* sink) |
| 686 |
{ |
| 687 |
wxString filename = wxFileName::CreateTempFileName("clTempFile"); |
| 688 |
wxString theCommand = wxString::Format("%s > \"%s\" 2>&1", command, filename); |
| 689 |
WrapInShell(theCommand); |
| 690 |
long rc = ::wxExecute(theCommand, wxEXEC_ASYNC | wxEXEC_HIDE_CONSOLE, new ProcessHelper(sink, filename), nullptr); |
| 691 |
if(rc > 0) { |
| 692 |
*pid = rc; |
| 693 |
} |
| 694 |
return rc > 0; |
| 695 |
} |
| 696 |
|
| 697 |
int ProcUtils::ShellExecSync(const wxString& command, wxString* output) |
| 698 |
{ |
| 699 |
wxString filename = wxFileName::CreateTempFileName("clTempFile"); |
| 700 |
wxString theCommand = wxString::Format("%s > \"%s\" 2>&1", command, filename); |
| 701 |
WrapInShell(theCommand); |
| 702 |
|
| 703 |
wxArrayString out; |
| 704 |
wxArrayString err; |
| 705 |
int exit_code = ::wxExecute(theCommand, out, err, wxEXEC_SYNC | wxEXEC_HIDE_CONSOLE); |
| 706 |
|
| 707 |
// read the process output and return it |
| 708 |
FileUtils::Deleter d{ filename }; |
| 709 |
FileUtils::ReadFileContent(filename, *output); |
| 710 |
return exit_code; |
| 711 |
} |
| 712 |
|
| 713 |
wxString& ProcUtils::WrapInShell(wxString& cmd) |
| 714 |
{ |
| 715 |
wxString command; |
| 716 |
#ifdef __WXMSW__ |
| 717 |
wxString shell = wxGetenv("COMSPEC"); |
| 718 |
if(shell.IsEmpty()) { |
| 719 |
shell = "CMD.EXE"; |
| 720 |
} |
| 721 |
command << shell << " /C "; |
| 722 |
if(cmd.StartsWith("\"") && !cmd.EndsWith("\"")) { |
| 723 |
command << "\"" << cmd << "\""; |
| 724 |
} else { |
| 725 |
command << cmd; |
| 726 |
} |
| 727 |
#else |
| 728 |
command << "/bin/sh -c '"; |
| 729 |
// escape any single quoutes |
| 730 |
cmd.Replace("'", "\\'"); |
| 731 |
command << cmd << "'"; |
| 732 |
#endif |
| 733 |
cmd.swap(command); |
| 734 |
return cmd; |
| 735 |
} |