Skip to content

Commit 45760a4

Browse files
author
rblazek
committedApr 6, 2006
read/write shell history
git-svn-id: http://svn.osgeo.org/qgis/trunk@5193 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 534abc8 commit 45760a4

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
 

‎src/plugins/grass/qgsgrassshell.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ extern "C" {
4646
#include <sys/stat.h>
4747
#include <sys/ioctl.h>
4848
#include <fcntl.h>
49+
#include <signal.h>
50+
#include <sys/wait.h>
4951
#endif //!WIN32
5052
}
5153

@@ -219,6 +221,8 @@ QgsGrassShell::QgsGrassShell ( QgsGrassTools *tools,
219221
exit(1);
220222
}
221223

224+
mPid = pid;
225+
222226
// Create socket notifier
223227
mOutNotifier = new QSocketNotifier ( mFdMaster, QSocketNotifier::Read, this);
224228

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

240244
QgsGrassShell::~QgsGrassShell()
241245
{
246+
#ifdef QGISDEBUG
247+
std::cerr << "QgsGrassShell::~QgsGrassShell()" << std::endl;
248+
#endif
249+
250+
// TODO: find signal to write history before exit
251+
// instead of sending 'exit'
252+
253+
int ret = write( mFdMaster, "exit\015\012", 6);
254+
int status;
255+
256+
while ( 1 )
257+
{
258+
readStdout(0);
259+
if ( waitpid ( mPid, &status, WNOHANG ) > 0 ) break;
260+
261+
struct timespec t, r;
262+
t.tv_sec = 0;
263+
t.tv_nsec = 10000000; // 0.01 s
264+
nanosleep ( &t, &r );
265+
}
266+
267+
/*
268+
std::cerr << "kill shell pid = " << mPid << std::endl;
269+
if ( kill(mPid,SIGTERM ) == -1 )
270+
{
271+
std::cerr << "cannot kill shell pid = " << mPid << std::endl;
272+
}
273+
*/
242274
}
243275

244276
void QgsGrassShell::keyPressEvent( QKeyEvent * e )

‎src/plugins/grass/qgsgrassshell.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ public slots:
153153
// Buffer for data read form shell stderr
154154
Q3CString mStderrBuffer;
155155

156+
// Shell process PID
157+
int mPid;
158+
156159
// Modes
157160
bool mMode[ModeCount];
158161

‎src/plugins/grass/qgsgrasstools.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ QgsGrassTools::QgsGrassTools ( QgisApp *qgisApp, QgisIface *iface,
109109
mIface = iface;
110110
mCanvas = mIface->getMapCanvas();
111111

112+
connect( qApp, SIGNAL(aboutToQuit()),
113+
this, SLOT(closeTools()) );
114+
112115
mTabWidget = new QgsGrassToolsTabWidget (this);
113116
QVBoxLayout *layout1 = new QVBoxLayout(this);
114117
layout1->addWidget(mTabWidget);
@@ -184,6 +187,25 @@ void QgsGrassTools::moduleClicked( Q3ListViewItem * item )
184187
QgsGrassShell *sh = 0;
185188
if ( name == "shell" )
186189
{
190+
// Set history file
191+
QString mapsetPath = QgsGrass::getDefaultGisdbase() + "/"
192+
+ QgsGrass::getDefaultLocation() + "/"
193+
+ QgsGrass::getDefaultMapset();
194+
195+
// bash
196+
QString hist = "HISTFILE=" + mapsetPath + "/.bash_history";
197+
char *histChar = new char[hist.length()+1];
198+
strcpy ( histChar, const_cast<char *>(hist.ascii()) );
199+
putenv( histChar );
200+
201+
// csh/tcsh
202+
#ifndef WIN32
203+
hist = "histfile=" + mapsetPath + "/.history";
204+
histChar = new char[hist.length()+1];
205+
strcpy ( histChar, const_cast<char *>(hist.ascii()) );
206+
putenv( histChar );
207+
#endif
208+
187209
#ifdef WIN32
188210
// Run MSYS if available
189211
// Note: I was not able to run cmd.exe and command.com
@@ -422,3 +444,15 @@ void QgsGrassTools::emitRegionChanged()
422444
#endif
423445
emit regionChanged();
424446
}
447+
448+
void QgsGrassTools::closeTools()
449+
{
450+
#ifdef QGISDEBUG
451+
std::cerr << "QgsGrassTools::closeTools()" << std::endl;
452+
#endif
453+
454+
for ( int i = mTabWidget->count()-1; i > 1; i-- )
455+
{
456+
delete mTabWidget->widget(i);
457+
}
458+
}

0 commit comments

Comments
 (0)
Please sign in to comment.