Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / konqueror / shellcmdplugin / kshellcmdexecutor.cpp
blobcdf2687a559f9e99d2497fc3e98176b3726f8a91
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"
21 #include <sys/time.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <stdlib.h>
27 #include <QtCore/QSocketNotifier>
29 #include <kinputdialog.h>
30 #include <kglobalsettings.h>
31 #include <kdesu/process.h>
32 #include <klocale.h>
34 using namespace KDESu;
36 KShellCommandExecutor::KShellCommandExecutor(const QString& command, QWidget* parent)
37 :QTextEdit(parent)
38 ,m_shellProcess(0)
39 ,m_command(command)
40 ,m_readNotifier(0)
41 ,m_writeNotifier(0)
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()";
59 setText("");
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;
74 args+="-c";
75 args+=m_command.toLocal8Bit();
76 //kDebug()<<"------- executing: "<<m_command.toLocal8Bit();
78 QByteArray shell( getenv("SHELL") );
79 if (shell.isEmpty())
80 shell = "sh";
82 int ret = m_shellProcess->exec(shell, args);
83 if (ret < 0)
85 //kDebug()<<"could not execute";
86 return 0;
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()));
95 return 1;
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
104 //process exited
105 if (bytesRead<=0)
107 slotFinished();
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 ------------";
121 bool ok;
122 QString str = KInputDialog::getText( QString(),
123 i18n( "Input Required:" ), QString(), &ok, this );
124 if ( ok )
126 QByteArray input=str.toLocal8Bit();
127 ::write(m_shellProcess->fd(),input,input.length());
128 ::write(m_shellProcess->fd(),"\n",1);
130 else
131 slotFinished();
133 if (m_writeNotifier)
135 m_writeNotifier->setEnabled(false);
139 void KShellCommandExecutor::slotFinished()
141 setTextFormat(Qt::PlainText);
142 if (m_shellProcess!=0)
144 delete m_readNotifier;
145 m_readNotifier = 0;
146 delete m_writeNotifier;
147 m_writeNotifier = 0;
149 //kDebug()<<"slotFinished: pid: "<<m_shellProcess->pid();
150 ::kill(m_shellProcess->pid()+1, SIGTERM);
151 ::kill(m_shellProcess->pid(), SIGTERM);
153 delete m_shellProcess;
154 m_shellProcess=0;
155 emit finished();
158 #include "kshellcmdexecutor.moc"