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 setAcceptRichText( false );
44 setFont( KGlobalSettings::fixedFont() );
48 KShellCommandExecutor::~KShellCommandExecutor()
50 if (m_shellProcess
!=0)
52 ::kill(m_shellProcess
->pid()+1, SIGTERM
);
53 delete m_shellProcess
;
57 int KShellCommandExecutor::exec()
59 //kDebug()<<"---------- KShellCommandExecutor::exec()";
61 if (m_shellProcess
!=0)
63 ::kill(m_shellProcess
->pid(),SIGTERM
);
64 delete m_shellProcess
;
66 delete m_readNotifier
;
67 delete m_writeNotifier
;
69 m_shellProcess
=new PtyProcess();
70 m_shellProcess
->setTerminal(true);
72 QList
<QByteArray
> args
;
74 args
+=m_command
.toLocal8Bit();
75 //kDebug()<<"------- executing: "<<m_command.toLocal8Bit();
77 QByteArray
shell( getenv("SHELL") );
81 int ret
= m_shellProcess
->exec(shell
, args
);
84 //kDebug()<<"could not execute";
85 delete m_shellProcess
;
90 m_readNotifier
=new QSocketNotifier(m_shellProcess
->fd(),QSocketNotifier::Read
, this);
91 m_writeNotifier
=new QSocketNotifier(m_shellProcess
->fd(),QSocketNotifier::Write
, this);
92 m_writeNotifier
->setEnabled(false);
93 connect (m_readNotifier
, SIGNAL(activated(int)), this,SLOT(readDataFromShell()));
94 connect (m_writeNotifier
, SIGNAL(activated(int)), this,SLOT(writeDataToShell()));
99 void KShellCommandExecutor::readDataFromShell()
101 //kDebug()<<"--------- reading ------------";
102 char buffer
[16*1024];
103 int bytesRead
=::read(m_shellProcess
->fd(), buffer
, 16*1024-1);
104 //0-terminate the buffer
110 else if (bytesRead
>0)
112 //kDebug()<<"***********************\n"<<buffer<<"###################\n";
113 buffer
[bytesRead
]='\0';
114 this->append(QString::fromLocal8Bit(buffer
));
115 setAcceptRichText( false );
119 void KShellCommandExecutor::writeDataToShell()
121 //kDebug()<<"--------- writing ------------";
123 QString str
= KInputDialog::getText( QString(),
124 i18n( "Input Required:" ), QString(), &ok
, this );
127 QByteArray input
=str
.toLocal8Bit();
128 ::write(m_shellProcess
->fd(),input
,input
.length());
129 ::write(m_shellProcess
->fd(),"\n",1);
136 m_writeNotifier
->setEnabled(false);
140 void KShellCommandExecutor::slotFinished()
142 setAcceptRichText( false );
143 if (m_shellProcess
!=0)
145 delete m_readNotifier
;
147 delete m_writeNotifier
;
150 //kDebug()<<"slotFinished: pid: "<<m_shellProcess->pid();
151 ::kill(m_shellProcess
->pid()+1, SIGTERM
);
152 ::kill(m_shellProcess
->pid(), SIGTERM
);
154 delete m_shellProcess
;
159 #include "kshellcmdexecutor.moc"