1 /* This file is part of the KDE project
2 Copyright (C) 2000 Alexander Neundorf <neundorf@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
19 #include "kshellcmdexecutor.h"
22 #include <sys/types.h>
27 #include <QtCore/QSocketNotifier>
29 #include <kinputdialog.h>
30 #include <kglobalsettings.h>
31 #include <kdesu/process.h>
34 using namespace KDESu
;
36 KShellCommandExecutor::KShellCommandExecutor(const QString
& command
, QWidget
* parent
)
43 setTextFormat(Qt::PlainText
);
44 setFont( KGlobalSettings::fixedFont() );
47 KShellCommandExecutor::~KShellCommandExecutor()
49 if (m_shellProcess
!=0)
51 ::kill(m_shellProcess
->pid()+1, SIGTERM
);
52 delete m_shellProcess
;
56 int KShellCommandExecutor::exec()
58 //kDebug()<<"---------- KShellCommandExecutor::exec()";
60 if (m_shellProcess
!=0)
62 ::kill(m_shellProcess
->pid(),SIGTERM
);
63 delete m_shellProcess
;
65 if (m_readNotifier
!=0)
66 delete m_readNotifier
;
67 if (m_writeNotifier
!=0)
68 delete m_writeNotifier
;
70 m_shellProcess
=new PtyProcess();
71 m_shellProcess
->setTerminal(true);
73 QList
<QByteArray
> args
;
75 args
+=m_command
.toLocal8Bit();
76 //kDebug()<<"------- executing: "<<m_command.toLocal8Bit();
78 QByteArray
shell( getenv("SHELL") );
82 int ret
= m_shellProcess
->exec(shell
, args
);
85 //kDebug()<<"could not execute";
89 m_readNotifier
=new QSocketNotifier(m_shellProcess
->fd(),QSocketNotifier::Read
, this);
90 m_writeNotifier
=new QSocketNotifier(m_shellProcess
->fd(),QSocketNotifier::Write
, this);
91 m_writeNotifier
->setEnabled(false);
92 connect (m_readNotifier
, SIGNAL(activated(int)), this,SLOT(readDataFromShell()));
93 connect (m_writeNotifier
, SIGNAL(activated(int)), this,SLOT(writeDataToShell()));
98 void KShellCommandExecutor::readDataFromShell()
100 //kDebug()<<"--------- reading ------------";
101 char buffer
[16*1024];
102 int bytesRead
=::read(m_shellProcess
->fd(), buffer
, 16*1024-1);
103 //0-terminate the buffer
109 else if (bytesRead
>0)
111 //kDebug()<<"***********************\n"<<buffer<<"###################\n";
112 buffer
[bytesRead
]='\0';
113 this->append(QString::fromLocal8Bit(buffer
));
114 setTextFormat(Qt::PlainText
);
118 void KShellCommandExecutor::writeDataToShell()
120 //kDebug()<<"--------- writing ------------";
122 QString str
= KInputDialog::getText( QString(),
123 i18n( "Input Required:" ), QString(), &ok
, this );
126 QByteArray input
=str
.toLocal8Bit();
127 ::write(m_shellProcess
->fd(),input
,input
.length());
128 ::write(m_shellProcess
->fd(),"\n",1);
135 m_writeNotifier
->setEnabled(false);
139 void KShellCommandExecutor::slotFinished()
141 setTextFormat(Qt::PlainText
);
142 if (m_shellProcess
!=0)
144 delete m_readNotifier
;
146 delete m_writeNotifier
;
149 //kDebug()<<"slotFinished: pid: "<<m_shellProcess->pid();
150 ::kill(m_shellProcess
->pid()+1, SIGTERM
);
151 ::kill(m_shellProcess
->pid(), SIGTERM
);
153 delete m_shellProcess
;
158 #include "kshellcmdexecutor.moc"