File indexing completed on 2023-09-24 04:10:19
0001 /* This file is part of the KDE libraries 0002 Copyright (C) 2004, 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org> 0003 Copyright (C) 2004, 2005, 2006 Matt Broadstone <mbroadst@gmail.com> 0004 Copyright (C) 2004, 2005, 2006 Richard J. Moore <rich@kde.org> 0005 Copyright (C) 2004, 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 0023 #include <QApplication> 0024 #include <QDebug> 0025 #include <QStringList> 0026 0027 #include <kjs/interpreter.h> 0028 #include <kjs/ustring.h> 0029 0030 #include <kjsembed/kjseglobal.h> 0031 #include <kjsembed/kjsembed.h> 0032 0033 #include <QDate> 0034 0035 using namespace KJSEmbed; 0036 0037 void printUsage(QString appName) 0038 { 0039 (*KJSEmbed::conerr()) << "Usage: " << appName << " [options] [file]" << Qt::endl 0040 << "Options:" << Qt::endl 0041 << " -e, --exec execute script without gui support." << Qt::endl 0042 << " -i, --interactive start interactive kjs interpreter." << Qt::endl 0043 << Qt::endl; 0044 } 0045 0046 int main(int argc, char **argv) 0047 { 0048 QTime time; 0049 time.start(); 0050 0051 #ifdef _WIN32 0052 # ifdef CONSOLEIO 0053 RedirectIOToConsole(); 0054 # endif 0055 #endif 0056 0057 // Handle arguments 0058 QString appName = argv[0]; 0059 QStringList args; 0060 for (int i = 1; i < argc; i++) { 0061 args << argv[i]; 0062 } 0063 0064 QString script; 0065 KJS::List scriptArgs; 0066 bool gui = true; 0067 0068 if (argc > 1) { 0069 while (!args.isEmpty()) { 0070 QString arg = args.takeFirst(); 0071 if (arg.contains('-')) { 0072 if ((arg == "--version") || (arg == "-v")) { 0073 printf("Qt: %s\n", qVersion()); 0074 return 0; 0075 } 0076 if ((arg == "--exec") || (arg == "-e")) { 0077 gui = false; 0078 } else if ((arg == "--interactive") || (arg == "-i")) { 0079 (*KJSEmbed::conout()) << "Interactive"; 0080 } else { 0081 printUsage(appName); 0082 return 0; 0083 } 0084 } else { 0085 if (!script.isEmpty()) { 0086 scriptArgs.append(KJS::jsString(arg)); 0087 } else { 0088 script = arg; 0089 } 0090 } 0091 } 0092 } else { 0093 printUsage(appName); 0094 return 0; 0095 } 0096 0097 // Setup QApplication 0098 QCoreApplication *app; 0099 0100 if (gui) { 0101 app = new QApplication(argc, argv); 0102 QObject::connect(app, SIGNAL(lastWindowClosed()), app, SLOT(quit())); 0103 } else { 0104 qDebug("no GUI"); 0105 app = new QCoreApplication(argc, argv); 0106 } 0107 qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed()); 0108 0109 app->setApplicationName(appName); 0110 0111 // Setup Interpreter 0112 time.restart(); 0113 Engine kernel; 0114 qDebug(" New engine %dms", time.elapsed()); 0115 time.restart(); 0116 0117 KJS::Interpreter *js = kernel.interpreter(); 0118 js->setShouldPrintExceptions(true); 0119 KJS::ExecState *exec = js->globalExec(); 0120 0121 // Publish bindings 0122 KJS::JSObject *appObject = kernel.addObject(app, "Application"); 0123 KJS::JSObject *argObject = js->builtinArray()->construct(exec, scriptArgs); 0124 appObject->put(exec, "args", argObject); 0125 Engine::ExitStatus result = Engine::Failure; 0126 0127 if (!script.isEmpty()) { 0128 result = kernel.runFile(toUString(script)); 0129 } else { // exec shell 0130 result = kernel.runFile(":/console.js"); 0131 } 0132 0133 if (result != Engine::Success) { 0134 KJS::Completion jsres = kernel.completion(); 0135 (*KJSEmbed::conerr()) << toQString(jsres.value()->toString(exec)) << Qt::endl; 0136 } 0137 return (int)result; 0138 } 0139