Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
read/write shell history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5193 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed Apr 6, 2006
1 parent 8e7f09c commit c4ca867
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/plugins/grass/qgsgrassshell.cpp
Expand Up @@ -46,6 +46,8 @@ extern "C" {
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#endif //!WIN32
}

Expand Down Expand Up @@ -219,6 +221,8 @@ QgsGrassShell::QgsGrassShell ( QgsGrassTools *tools,
exit(1);
}

mPid = pid;

// Create socket notifier
mOutNotifier = new QSocketNotifier ( mFdMaster, QSocketNotifier::Read, this);

Expand All @@ -239,6 +243,34 @@ QgsGrassShell::QgsGrassShell ( QgsGrassTools *tools,

QgsGrassShell::~QgsGrassShell()
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassShell::~QgsGrassShell()" << std::endl;
#endif

// TODO: find signal to write history before exit
// instead of sending 'exit'

int ret = write( mFdMaster, "exit\015\012", 6);
int status;

while ( 1 )
{
readStdout(0);
if ( waitpid ( mPid, &status, WNOHANG ) > 0 ) break;

struct timespec t, r;
t.tv_sec = 0;
t.tv_nsec = 10000000; // 0.01 s
nanosleep ( &t, &r );
}

/*
std::cerr << "kill shell pid = " << mPid << std::endl;
if ( kill(mPid,SIGTERM ) == -1 )
{
std::cerr << "cannot kill shell pid = " << mPid << std::endl;
}
*/
}

void QgsGrassShell::keyPressEvent( QKeyEvent * e )
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/grass/qgsgrassshell.h
Expand Up @@ -153,6 +153,9 @@ public slots:
// Buffer for data read form shell stderr
Q3CString mStderrBuffer;

// Shell process PID
int mPid;

// Modes
bool mMode[ModeCount];

Expand Down
34 changes: 34 additions & 0 deletions src/plugins/grass/qgsgrasstools.cpp
Expand Up @@ -109,6 +109,9 @@ QgsGrassTools::QgsGrassTools ( QgisApp *qgisApp, QgisIface *iface,
mIface = iface;
mCanvas = mIface->getMapCanvas();

connect( qApp, SIGNAL(aboutToQuit()),
this, SLOT(closeTools()) );

mTabWidget = new QgsGrassToolsTabWidget (this);
QVBoxLayout *layout1 = new QVBoxLayout(this);
layout1->addWidget(mTabWidget);
Expand Down Expand Up @@ -184,6 +187,25 @@ void QgsGrassTools::moduleClicked( Q3ListViewItem * item )
QgsGrassShell *sh = 0;
if ( name == "shell" )
{
// Set history file
QString mapsetPath = QgsGrass::getDefaultGisdbase() + "/"
+ QgsGrass::getDefaultLocation() + "/"
+ QgsGrass::getDefaultMapset();

// bash
QString hist = "HISTFILE=" + mapsetPath + "/.bash_history";
char *histChar = new char[hist.length()+1];
strcpy ( histChar, const_cast<char *>(hist.ascii()) );
putenv( histChar );

// csh/tcsh
#ifndef WIN32
hist = "histfile=" + mapsetPath + "/.history";
histChar = new char[hist.length()+1];
strcpy ( histChar, const_cast<char *>(hist.ascii()) );
putenv( histChar );
#endif

#ifdef WIN32
// Run MSYS if available
// Note: I was not able to run cmd.exe and command.com
Expand Down Expand Up @@ -422,3 +444,15 @@ void QgsGrassTools::emitRegionChanged()
#endif
emit regionChanged();
}

void QgsGrassTools::closeTools()
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassTools::closeTools()" << std::endl;
#endif

for ( int i = mTabWidget->count()-1; i > 1; i-- )
{
delete mTabWidget->widget(i);
}
}

0 comments on commit c4ca867

Please sign in to comment.