File indexing completed on 2024-04-28 15:28:44

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 #include "iosupport.h"
0023 #include "static_binding.h"
0024 #include "kjseglobal.h"
0025 
0026 #include <kjs/object.h>
0027 #include <QDebug>
0028 #include <QStandardPaths>
0029 
0030 #include <QStringList>
0031 #include <QProcess>
0032 
0033 using namespace KJSEmbed;
0034 
0035 KJS::JSValue *callPrint(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0036 {
0037     (*KJSEmbed::conout()) << toQString(args[0]->toString(exec));
0038     return KJS::jsNull();
0039 }
0040 
0041 KJS::JSValue *callPrintLn(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0042 {
0043     (*KJSEmbed::conout()) << toQString(args[0]->toString(exec)) << Qt::endl;
0044     return KJS::jsNull();
0045 }
0046 
0047 KJS::JSValue *callDebug(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0048 {
0049     //(*KJSEmbed::conerr())  << "Debug: " << toQString(args[0]->toString(exec)) << Qt::endl;
0050     qDebug()  << "Debug: " << toQString(args[0]->toString(exec));
0051     return KJS::jsNull();
0052 }
0053 
0054 KJS::JSValue *callReadLine(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0055 {
0056     Q_UNUSED(exec);
0057     Q_UNUSED(args);
0058     QString line = conin()->readLine();
0059     return KJS::jsString(line);
0060 }
0061 
0062 KJS::JSValue *callSystem(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args)
0063 {
0064     QStringList processArgs = toQString(args[0]->toString(exec)).split(' ');
0065     QString app = processArgs[0];
0066     processArgs.pop_front();
0067 
0068     const QString executable = QStandardPaths::findExecutable(app);
0069     if (executable.isEmpty()) {
0070         return KJS::throwError(exec, KJS::GeneralError, "Could not find appliction.");
0071     }
0072 
0073     QProcess systemProcess;
0074     systemProcess.start(executable, processArgs);
0075     if (!systemProcess.waitForStarted()) {
0076         return KJS::throwError(exec, KJS::GeneralError, "Application could not start.");
0077     }
0078     if (!systemProcess.waitForFinished()) {
0079         return KJS::throwError(exec, KJS::GeneralError, "Application crashed.");
0080     }
0081     return KJS::jsString(systemProcess.readAll().data());
0082 }
0083 
0084 const Method IoFactory::IoMethods[] = {
0085     {"debug", 1, KJS::DontDelete | KJS::ReadOnly, &callDebug },
0086     {"print", 1, KJS::DontDelete | KJS::ReadOnly, &callPrint },
0087     {"println", 1, KJS::DontDelete | KJS::ReadOnly, &callPrintLn },
0088     {"readln", 0, KJS::DontDelete | KJS::ReadOnly, &callReadLine },
0089     {"system", 1, KJS::DontDelete | KJS::ReadOnly, &callSystem },
0090     {nullptr, 0, 0, nullptr }
0091 };
0092