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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : consolefinder.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "consolefinder.h"
#include "procutils.h"
#include "exelocator.h"
#include "macros.h"
ConsoleFinder::ConsoleFinder()
: m_nConsolePid(0)
, m_consoleCommand(TERMINAL_CMD)
{
}
ConsoleFinder::~ConsoleFinder()
{
FreeConsole();
}
void ConsoleFinder::FreeConsole()
{
if (m_nConsolePid) {
wxKill(m_nConsolePid, wxSIGKILL, NULL, wxKILL_CHILDREN);
m_nConsolePid = 0;
}
}
int ConsoleFinder::RunConsole(const wxString &title)
{
// start the xterm and put the shell to sleep with -e sleep 80000
// fetch the xterm tty so we can issue to gdb a "tty /dev/pts/#"
// redirecting program stdin/stdout/stderr to the xterm console.
#ifndef __WXMSW__
wxString cmd;
cmd = GetConsoleCommand();
cmd.Replace(wxT("$(TITLE)"), title);
cmd.Replace(wxT("$(CMD)"), wxString::Format(wxT("sleep %lu"), 80000 + wxGetProcessId()));
wxLogMessage(wxString::Format(wxT("Launching console: %s"), cmd.c_str()));
m_nConsolePid = wxExecute(cmd, wxEXEC_ASYNC|wxEXEC_MAKE_GROUP_LEADER);
if (m_nConsolePid <= 0) {
return -1;
}
// Issue the PS command to get the /dev/tty device name
// First, wait for the xterm to settle down, else PS won't see the sleep task
wxSleep(1);
m_ConsoleTty = GetConsoleTty(m_nConsolePid);
if (m_ConsoleTty.IsEmpty()) {
FreeConsole();
return -1;
}
return m_nConsolePid;
#else //__WXMSW__
wxUnusedVar(title);
return -1;
#endif
}
wxString ConsoleFinder::GetConsoleTty(int ConsolePid)
{
#ifndef __WXMSW__
// execute the ps x -o command and read PS output to get the /dev/tty field
unsigned long ConsPid = ConsolePid;
wxString psCmd;
wxArrayString psOutput;
wxArrayString psErrors;
psCmd << wxT("ps x -o tty,pid,command");
ProcUtils::ExecuteCommand(psCmd, psOutput);
wxString ConsTtyStr;
wxString ConsPidStr;
ConsPidStr << ConsPid;
//find task with our unique sleep time
wxString uniqueSleepTimeStr;
uniqueSleepTimeStr << wxT("sleep ") << wxString::Format(wxT("%lu"),80000 + ::wxGetProcessId());
// search the output of "ps pid" command
int knt = psOutput.GetCount();
for (int i=knt-1; i>-1; --i) {
psCmd = psOutput.Item(i);
// find the pts/# or tty/# or whatever it's called
// by seaching the output of "ps x -o tty,pid,command" command.
// The output of ps looks like:
// TT PID COMMAND
// pts/0 13342 /bin/sh ./run.sh
// pts/0 13343 /home/pecanpecan/devel/trunk/src/devel/codeblocks
// pts/0 13361 /usr/bin/gdb -nx -fullname -quiet -args ./conio
// pts/0 13362 xterm -font -*-*-*-*-*-*-20-*-*-*-*-*-*-* -T Program Console -e sleep 93343
// pts/2 13363 sleep 93343
// ? 13365 /home/pecan/proj/conio/conio
// pts/1 13370 ps x -o tty,pid,command
if (psCmd.Contains(uniqueSleepTimeStr))
do { // check for correct "sleep" line
if (psCmd.Contains(wxT("-T"))) break; //error;wrong sleep line.
// found "sleep 93343" string, extract tty field
ConsTtyStr = wxT("/dev/") + psCmd.BeforeFirst(' ');
return ConsTtyStr;
} while (0);//if do
}//for
return wxEmptyString;
#else
wxUnusedVar(ConsolePid);
return wxEmptyString;
#endif
}
bool ConsoleFinder::FindConsole(const wxString &title, wxString &consoleName)
{
int pid = RunConsole(title);
if (pid > 0) {
consoleName = m_ConsoleTty;
return true;
}
return false;
}
wxString ConsoleFinder::GetConsoleName()
{
wxString cmd;
#ifdef __WXMSW__
cmd = wxGetenv(wxT("COMSPEC"));
if ( cmd.IsEmpty() ) {
cmd = wxT("\\COMMAND.COM");
}
#else //non-windows
//try to locate the default terminal
wxString terminal;
wxString where;
if (ExeLocator::Locate(wxT("x-terminal-emulator"), where)) {
terminal = wxT("x-terminal-emulator -e ");
} else if (ExeLocator::Locate(wxT("gnome-terminal"), where)) {
terminal = wxT("gnome-terminal -e ");
} else if (ExeLocator::Locate(wxT("konsole"), where)) {
terminal = wxT("konsole");
} else if (ExeLocator::Locate(wxT("terminal"), where)) {
terminal = wxT("terminal -e");
} else if (ExeLocator::Locate(wxT("lxterminal"), where)) {
terminal = wxT("lxterminal -e");
} else if (ExeLocator::Locate(wxT("xterm"), where)) {
terminal = wxT("xterm -e ");
}
if (cmd.IsEmpty()) {
cmd = wxT("xterm -e ");
}
cmd = terminal;
#endif
return cmd;
}
|