1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 |
|
---|
43 | #include <qscriptengine.h>
|
---|
44 |
|
---|
45 | #include <QtCore/QFile>
|
---|
46 | #include <QtCore/QTextStream>
|
---|
47 | #include <QtCore/QStringList>
|
---|
48 | #include <QtGui/QApplication>
|
---|
49 |
|
---|
50 | #include <stdlib.h>
|
---|
51 |
|
---|
52 | #include "bytearrayclass.h"
|
---|
53 |
|
---|
54 | static bool wantsToQuit;
|
---|
55 |
|
---|
56 | static QScriptValue qtscript_quit(QScriptContext *ctx, QScriptEngine *eng)
|
---|
57 | {
|
---|
58 | Q_UNUSED(ctx);
|
---|
59 | wantsToQuit = true;
|
---|
60 | return eng->undefinedValue();
|
---|
61 | }
|
---|
62 |
|
---|
63 | static void interactive(QScriptEngine *eng)
|
---|
64 | {
|
---|
65 | QScriptValue global = eng->globalObject();
|
---|
66 | QScriptValue quitFunction = eng->newFunction(qtscript_quit);
|
---|
67 | if (!global.property(QLatin1String("exit")).isValid())
|
---|
68 | global.setProperty(QLatin1String("exit"), quitFunction);
|
---|
69 | if (!global.property(QLatin1String("quit")).isValid())
|
---|
70 | global.setProperty(QLatin1String("quit"), quitFunction);
|
---|
71 | wantsToQuit = false;
|
---|
72 |
|
---|
73 | QTextStream qin(stdin, QFile::ReadOnly);
|
---|
74 |
|
---|
75 | const char *qscript_prompt = "qs> ";
|
---|
76 | const char *dot_prompt = ".... ";
|
---|
77 | const char *prompt = qscript_prompt;
|
---|
78 |
|
---|
79 | QString code;
|
---|
80 |
|
---|
81 | forever {
|
---|
82 | QString line;
|
---|
83 |
|
---|
84 | printf("%s", prompt);
|
---|
85 | fflush(stdout);
|
---|
86 |
|
---|
87 | line = qin.readLine();
|
---|
88 | if (line.isNull())
|
---|
89 | break;
|
---|
90 |
|
---|
91 | code += line;
|
---|
92 | code += QLatin1Char('\n');
|
---|
93 |
|
---|
94 | if (line.trimmed().isEmpty()) {
|
---|
95 | continue;
|
---|
96 |
|
---|
97 | } else if (! eng->canEvaluate(code)) {
|
---|
98 | prompt = dot_prompt;
|
---|
99 |
|
---|
100 | } else {
|
---|
101 | QScriptValue result = eng->evaluate(code, QLatin1String("typein"));
|
---|
102 |
|
---|
103 | code.clear();
|
---|
104 | prompt = qscript_prompt;
|
---|
105 |
|
---|
106 | if (! result.isUndefined())
|
---|
107 | fprintf(stderr, "%s\n", qPrintable(result.toString()));
|
---|
108 |
|
---|
109 | if (wantsToQuit)
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | static QScriptValue importExtension(QScriptContext *context, QScriptEngine *engine)
|
---|
116 | {
|
---|
117 | return engine->importExtension(context->argument(0).toString());
|
---|
118 | }
|
---|
119 |
|
---|
120 | static QScriptValue loadScripts(QScriptContext *context, QScriptEngine *engine)
|
---|
121 | {
|
---|
122 | for (int i = 0; i < context->argumentCount(); ++i) {
|
---|
123 | QString fileName = context->argument(0).toString();
|
---|
124 | QFile file(fileName);
|
---|
125 | if (!file.open(QIODevice::ReadOnly))
|
---|
126 | return context->throwError(QString::fromLatin1("could not open %0 for reading").arg(fileName));
|
---|
127 | QTextStream ts(&file);
|
---|
128 | QString contents = ts.readAll();
|
---|
129 | file.close();
|
---|
130 | QScriptContext *pc = context->parentContext();
|
---|
131 | context->setActivationObject(pc->activationObject());
|
---|
132 | context->setThisObject(pc->thisObject());
|
---|
133 | QScriptValue ret = engine->evaluate(contents);
|
---|
134 | if (engine->hasUncaughtException())
|
---|
135 | return ret;
|
---|
136 | }
|
---|
137 | return engine->undefinedValue();
|
---|
138 | }
|
---|
139 |
|
---|
140 | int main(int argc, char *argv[])
|
---|
141 | {
|
---|
142 | QApplication *app;
|
---|
143 | if (argc >= 2 && !qstrcmp(argv[1], "-tty")) {
|
---|
144 | ++argv;
|
---|
145 | --argc;
|
---|
146 | app = new QApplication(argc, argv, QApplication::Tty);
|
---|
147 | } else {
|
---|
148 | app = new QApplication(argc, argv);
|
---|
149 | }
|
---|
150 |
|
---|
151 | QScriptEngine *eng = new QScriptEngine();
|
---|
152 |
|
---|
153 | QScriptValue globalObject = eng->globalObject();
|
---|
154 |
|
---|
155 | globalObject.setProperty("load", eng->newFunction(loadScripts, /*length=*/1));
|
---|
156 |
|
---|
157 | {
|
---|
158 | if (!globalObject.property("qt").isObject())
|
---|
159 | globalObject.setProperty("qt", eng->newObject());
|
---|
160 | QScriptValue qscript = eng->newObject();
|
---|
161 | qscript.setProperty("importExtension", eng->newFunction(importExtension));
|
---|
162 | globalObject.property("qt").setProperty("script", qscript);
|
---|
163 | }
|
---|
164 |
|
---|
165 | ByteArrayClass *byteArrayClass = new ByteArrayClass(eng);
|
---|
166 | globalObject.setProperty("ByteArray", byteArrayClass->constructor());
|
---|
167 |
|
---|
168 | if (! *++argv) {
|
---|
169 | interactive(eng);
|
---|
170 | return EXIT_SUCCESS;
|
---|
171 | }
|
---|
172 |
|
---|
173 | while (const char *arg = *argv++) {
|
---|
174 | QString fn = QString::fromLocal8Bit(arg);
|
---|
175 |
|
---|
176 | if (fn == QLatin1String("-i")) {
|
---|
177 | interactive(eng);
|
---|
178 | break;
|
---|
179 | }
|
---|
180 |
|
---|
181 | QString contents;
|
---|
182 | int lineNumber = 1;
|
---|
183 |
|
---|
184 | if (fn == QLatin1String("-")) {
|
---|
185 | QTextStream stream(stdin, QFile::ReadOnly);
|
---|
186 | contents = stream.readAll();
|
---|
187 | }
|
---|
188 |
|
---|
189 | else {
|
---|
190 | QFile file(fn);
|
---|
191 |
|
---|
192 | if (file.open(QFile::ReadOnly)) {
|
---|
193 | QTextStream stream(&file);
|
---|
194 | contents = stream.readAll();
|
---|
195 | file.close();
|
---|
196 |
|
---|
197 | // strip off #!/usr/bin/env qscript line
|
---|
198 | if (contents.startsWith("#!")) {
|
---|
199 | contents.remove(0, contents.indexOf("\n"));
|
---|
200 | ++lineNumber;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (contents.isEmpty())
|
---|
206 | continue;
|
---|
207 |
|
---|
208 | QScriptValue r = eng->evaluate(contents, fn, lineNumber);
|
---|
209 | if (eng->hasUncaughtException()) {
|
---|
210 | QStringList backtrace = eng->uncaughtExceptionBacktrace();
|
---|
211 | fprintf (stderr, " %s\n%s\n\n", qPrintable(r.toString()),
|
---|
212 | qPrintable(backtrace.join("\n")));
|
---|
213 | return EXIT_FAILURE;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | delete eng;
|
---|
218 | delete app;
|
---|
219 |
|
---|
220 | return EXIT_SUCCESS;
|
---|
221 | }
|
---|