1 /* This file is part of the KDE libraries
2 Copyright (C) 2004, 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3 Copyright (C) 2004, 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4 Copyright (C) 2004, 2005, 2006 Richard J. Moore <rich@kde.org>
5 Copyright (C) 2004, 2005, 2006 Erik L. Bunce <kde@bunce.us>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
24 #include <QtGui/QApplication>
25 #include <QtCore/QDebug>
26 #include <QtCore/QStringList>
29 #include <kapplication.h>
30 #include <kaboutdata.h>
31 #include <kcmdlineargs.h>
32 #include <kdeversion.h>
35 #include <kjs/interpreter.h>
36 #include <kjs/ustring.h>
38 #include <kjsembed/kjseglobal.h>
39 #include <kjsembed/kjsembed.h>
41 #include <QtCore/QDate>
43 using namespace KJSEmbed
;
45 void printUsage(QString appName
)
47 (*KJSEmbed::conerr()) << "Usage: " << appName
<< " [options] [file]" << endl
49 << " -e, --exec execute script without gui support." << endl
50 << " -i, --interactive start interactive kjs interpreter." << endl
52 << " -n, --no-kde start without KDE KApplication support." << endl
61 int main( int argc
, char **argv
)
68 RedirectIOToConsole();
73 QString appName
= argv
[0];
75 for (int i
= 1; i
< argc
; i
++ )
86 #warning "KDE Support enabled"
93 #warning "KDE Support disabled"
100 while (!args
.isEmpty())
102 QString arg
= args
.takeFirst();
103 if (arg
.contains('-'))
105 if ((arg
== "--version") || (arg
== "-v"))
107 printf("Qt: %s\n", qVersion());
109 printf("KDE: %s\n", KDE_VERSION_STRING
);
113 if ((arg
== "--exec") || (arg
== "-e"))
117 else if ((arg
== "--interactive") || (arg
== "-i"))
118 (*KJSEmbed::conout()) << "Interactive";
120 else if ((arg
== "-n") || (arg
== "--no-kde"))
133 if (!script
.isEmpty())
134 scriptArgs
.append(KJS::jsString(arg
));
146 // Setup QApplication
147 QCoreApplication
*app
;
152 KAboutData
aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2",
154 "Utility for running KJSEmbed scripts \n" ),
155 KAboutData::License_LGPL
,
156 ki18n("(C) 2005-2006 The KJSEmbed Authors") );
158 KCmdLineOptions options
;
159 options
.add("e", ki18n("Execute script without gui support"));
160 options
.add("exec", ki18n("Execute script without gui support"));
161 options
.add("i", ki18n("start interactive kjs interpreter"));
162 options
.add("interactive", ki18n("start interactive kjs interpreter"));
163 options
.add("n", ki18n("start without KDE KApplication support."));
164 options
.add("no-kde", ki18n("start without KDE KApplication support."));
165 options
.add("!+command", ki18n("Script to execute"));
167 KCmdLineArgs::addCmdLineOptions( options
); // Add our own options.
168 KCmdLineArgs::init( argc
, argv
, &aboutData
);
170 app
= new KApplication(gui
);
177 app
= new QApplication( argc
, argv
);
178 dynamic_cast<QApplication
*>(app
)->connect( app
, SIGNAL( lastWindowClosed() ), SLOT(quit()) );
183 app
= new QCoreApplication(argc
, argv
);
185 qDebug(" New %s %dms", app
->metaObject()->className(), time
.elapsed());
187 app
->setApplicationName( appName
);
192 qDebug(" New engine %dms", time
.elapsed());
195 KJS::Interpreter
*js
= kernel
.interpreter();
196 js
->setShouldPrintExceptions(true);
197 KJS::ExecState
*exec
= js
->globalExec();
200 KJS::JSObject
*appObject
= kernel
.addObject( app
, "Application" );
201 KJS::JSObject
*argObject
= js
->builtinArray()->construct( exec
, scriptArgs
);
202 appObject
->put( exec
, "args", argObject
);
203 Engine::ExitStatus result
= Engine::Failure
;
205 if (!script
.isEmpty())
207 result
= kernel
.runFile(toUString(script
));
211 result
= kernel
.runFile( ":/console.js" );
214 if ( result
!= Engine::Success
)
216 KJS::Completion jsres
= kernel
.completion();
217 (*KJSEmbed::conerr()) << toQString(jsres
.value()->toString(exec
)) << endl
;