[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | #include <qscriptengine.h>
|
---|
| 43 |
|
---|
| 44 | #include <QtCore/QFile>
|
---|
| 45 | #include <QtCore/QTextStream>
|
---|
| 46 | #include <QtCore/QStringList>
|
---|
| 47 | #include <QtGui/QApplication>
|
---|
| 48 |
|
---|
| 49 | #include <stdlib.h>
|
---|
| 50 |
|
---|
| 51 | #include "bytearrayclass.h"
|
---|
| 52 |
|
---|
| 53 | static bool wantsToQuit;
|
---|
| 54 |
|
---|
| 55 | static QScriptValue qtscript_quit(QScriptContext *ctx, QScriptEngine *eng)
|
---|
| 56 | {
|
---|
| 57 | Q_UNUSED(ctx);
|
---|
| 58 | wantsToQuit = true;
|
---|
| 59 | return eng->undefinedValue();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | static void interactive(QScriptEngine *eng)
|
---|
| 63 | {
|
---|
| 64 | QScriptValue global = eng->globalObject();
|
---|
| 65 | QScriptValue quitFunction = eng->newFunction(qtscript_quit);
|
---|
| 66 | if (!global.property(QLatin1String("exit")).isValid())
|
---|
| 67 | global.setProperty(QLatin1String("exit"), quitFunction);
|
---|
| 68 | if (!global.property(QLatin1String("quit")).isValid())
|
---|
| 69 | global.setProperty(QLatin1String("quit"), quitFunction);
|
---|
| 70 | wantsToQuit = false;
|
---|
| 71 |
|
---|
| 72 | QTextStream qin(stdin, QFile::ReadOnly);
|
---|
| 73 |
|
---|
| 74 | const char *qscript_prompt = "qs> ";
|
---|
| 75 | const char *dot_prompt = ".... ";
|
---|
| 76 | const char *prompt = qscript_prompt;
|
---|
| 77 |
|
---|
| 78 | QString code;
|
---|
| 79 |
|
---|
| 80 | forever {
|
---|
| 81 | QString line;
|
---|
| 82 |
|
---|
| 83 | printf("%s", prompt);
|
---|
| 84 | fflush(stdout);
|
---|
| 85 |
|
---|
| 86 | line = qin.readLine();
|
---|
| 87 | if (line.isNull())
|
---|
| 88 | break;
|
---|
| 89 |
|
---|
| 90 | code += line;
|
---|
| 91 | code += QLatin1Char('\n');
|
---|
| 92 |
|
---|
| 93 | if (line.trimmed().isEmpty()) {
|
---|
| 94 | continue;
|
---|
| 95 |
|
---|
| 96 | } else if (! eng->canEvaluate(code)) {
|
---|
| 97 | prompt = dot_prompt;
|
---|
| 98 |
|
---|
| 99 | } else {
|
---|
| 100 | QScriptValue result = eng->evaluate(code, QLatin1String("typein"));
|
---|
| 101 |
|
---|
| 102 | code.clear();
|
---|
| 103 | prompt = qscript_prompt;
|
---|
| 104 |
|
---|
| 105 | if (! result.isUndefined())
|
---|
| 106 | fprintf(stderr, "%s\n", qPrintable(result.toString()));
|
---|
| 107 |
|
---|
| 108 | if (wantsToQuit)
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | static QScriptValue importExtension(QScriptContext *context, QScriptEngine *engine)
|
---|
| 115 | {
|
---|
| 116 | return engine->importExtension(context->argument(0).toString());
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | static QScriptValue loadScripts(QScriptContext *context, QScriptEngine *engine)
|
---|
| 120 | {
|
---|
| 121 | for (int i = 0; i < context->argumentCount(); ++i) {
|
---|
| 122 | QString fileName = context->argument(0).toString();
|
---|
| 123 | QFile file(fileName);
|
---|
| 124 | if (!file.open(QIODevice::ReadOnly))
|
---|
| 125 | return context->throwError(QString::fromLatin1("could not open %0 for reading").arg(fileName));
|
---|
| 126 | QTextStream ts(&file);
|
---|
| 127 | QString contents = ts.readAll();
|
---|
| 128 | file.close();
|
---|
| 129 | QScriptContext *pc = context->parentContext();
|
---|
| 130 | context->setActivationObject(pc->activationObject());
|
---|
| 131 | context->setThisObject(pc->thisObject());
|
---|
| 132 | QScriptValue ret = engine->evaluate(contents);
|
---|
| 133 | if (engine->hasUncaughtException())
|
---|
| 134 | return ret;
|
---|
| 135 | }
|
---|
| 136 | return engine->undefinedValue();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | int main(int argc, char *argv[])
|
---|
| 140 | {
|
---|
| 141 | QApplication *app;
|
---|
| 142 | if (argc >= 2 && !qstrcmp(argv[1], "-tty")) {
|
---|
| 143 | ++argv;
|
---|
| 144 | --argc;
|
---|
| 145 | app = new QApplication(argc, argv, QApplication::Tty);
|
---|
| 146 | } else {
|
---|
| 147 | app = new QApplication(argc, argv);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | QScriptEngine *eng = new QScriptEngine();
|
---|
| 151 |
|
---|
| 152 | QScriptValue globalObject = eng->globalObject();
|
---|
| 153 |
|
---|
| 154 | globalObject.setProperty("load", eng->newFunction(loadScripts, /*length=*/1));
|
---|
| 155 |
|
---|
| 156 | {
|
---|
| 157 | if (!globalObject.property("qt").isObject())
|
---|
| 158 | globalObject.setProperty("qt", eng->newObject());
|
---|
| 159 | QScriptValue qscript = eng->newObject();
|
---|
| 160 | qscript.setProperty("importExtension", eng->newFunction(importExtension));
|
---|
| 161 | globalObject.property("qt").setProperty("script", qscript);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | ByteArrayClass *byteArrayClass = new ByteArrayClass(eng);
|
---|
| 165 | globalObject.setProperty("ByteArray", byteArrayClass->constructor());
|
---|
| 166 |
|
---|
| 167 | if (! *++argv) {
|
---|
| 168 | interactive(eng);
|
---|
| 169 | return EXIT_SUCCESS;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | while (const char *arg = *argv++) {
|
---|
| 173 | QString fn = QString::fromLocal8Bit(arg);
|
---|
| 174 |
|
---|
| 175 | if (fn == QLatin1String("-i")) {
|
---|
| 176 | interactive(eng);
|
---|
| 177 | break;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | QString contents;
|
---|
| 181 | int lineNumber = 1;
|
---|
| 182 |
|
---|
| 183 | if (fn == QLatin1String("-")) {
|
---|
| 184 | QTextStream stream(stdin, QFile::ReadOnly);
|
---|
| 185 | contents = stream.readAll();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | else {
|
---|
| 189 | QFile file(fn);
|
---|
| 190 |
|
---|
| 191 | if (file.open(QFile::ReadOnly)) {
|
---|
| 192 | QTextStream stream(&file);
|
---|
| 193 | contents = stream.readAll();
|
---|
| 194 | file.close();
|
---|
| 195 |
|
---|
| 196 | // strip off #!/usr/bin/env qscript line
|
---|
| 197 | if (contents.startsWith("#!")) {
|
---|
| 198 | contents.remove(0, contents.indexOf("\n"));
|
---|
| 199 | ++lineNumber;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | if (contents.isEmpty())
|
---|
| 205 | continue;
|
---|
| 206 |
|
---|
| 207 | QScriptValue r = eng->evaluate(contents, fn, lineNumber);
|
---|
| 208 | if (eng->hasUncaughtException()) {
|
---|
| 209 | QStringList backtrace = eng->uncaughtExceptionBacktrace();
|
---|
| 210 | fprintf (stderr, " %s\n%s\n\n", qPrintable(r.toString()),
|
---|
| 211 | qPrintable(backtrace.join("\n")));
|
---|
| 212 | return EXIT_FAILURE;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | delete eng;
|
---|
| 217 | delete app;
|
---|
| 218 |
|
---|
| 219 | return EXIT_SUCCESS;
|
---|
| 220 | }
|
---|