add more spacing
[personal-kdebase.git] / apps / konqueror / shellcmdplugin / kshellcmdexecutor.cpp
blobc202fb05342596ba8075dbca8837d55a73f04531
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 setAcceptRichText( false );
44 setFont( KGlobalSettings::fixedFont() );
45 setReadOnly( true );
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()";
60 setText("");
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;
73 args+="-c";
74 args+=m_command.toLocal8Bit();
75 //kDebug()<<"------- executing: "<<m_command.toLocal8Bit();
77 QByteArray shell( getenv("SHELL") );
78 if (shell.isEmpty())
79 shell = "sh";
81 int ret = m_shellProcess->exec(shell, args);
82 if (ret < 0)
84 //kDebug()<<"could not execute";
85 delete m_shellProcess;
86 m_shellProcess = 0L;
87 return 0;
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()));
96 return 1;
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
105 //process exited
106 if (bytesRead<=0)
108 slotFinished();
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 ------------";
122 bool ok;
123 QString str = KInputDialog::getText( QString(),
124 i18n( "Input Required:" ), QString(), &ok, this );
125 if ( ok )
127 QByteArray input=str.toLocal8Bit();
128 ::write(m_shellProcess->fd(),input,input.length());
129 ::write(m_shellProcess->fd(),"\n",1);
131 else
132 slotFinished();
134 if (m_writeNotifier)
136 m_writeNotifier->setEnabled(false);
140 void KShellCommandExecutor::slotFinished()
142 setAcceptRichText( false );
143 if (m_shellProcess!=0)
145 delete m_readNotifier;
146 m_readNotifier = 0;
147 delete m_writeNotifier;
148 m_writeNotifier = 0;
150 //kDebug()<<"slotFinished: pid: "<<m_shellProcess->pid();
151 ::kill(m_shellProcess->pid()+1, SIGTERM);
152 ::kill(m_shellProcess->pid(), SIGTERM);
154 delete m_shellProcess;
155 m_shellProcess=0;
156 emit finished();
159 #include "kshellcmdexecutor.moc"