fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kjsembed / kjscmd / kjscmd.cpp
blob8e8813316965027fa71a3264ac954165f3db528d
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>
28 #ifndef QT_ONLY
29 #include <kapplication.h>
30 #include <kaboutdata.h>
31 #include <kcmdlineargs.h>
32 #include <kdeversion.h>
33 #endif // QT_ONLY
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
48 << "Options:" << endl
49 << " -e, --exec execute script without gui support." << endl
50 << " -i, --interactive start interactive kjs interpreter." << endl
51 #ifndef QT_ONLY
52 << " -n, --no-kde start without KDE KApplication support." << endl
53 #endif
54 << endl;
57 #ifndef QT_ONLY
59 #endif // QT_ONLY
61 int main( int argc, char **argv )
63 QTime time;
64 time.start();
66 #ifdef _WIN32
67 # ifdef CONSOLEIO
68 RedirectIOToConsole();
69 # endif
70 #endif
72 // Handle arguments
73 QString appName = argv[0];
74 QStringList args;
75 for (int i = 1; i < argc; i++ )
77 args << argv[i];
80 QString script;
81 KJS::List scriptArgs;
82 bool gui = true;
83 #ifndef QT_ONLY
85 #ifdef __GNUC__
86 #warning "KDE Support enabled"
87 #endif
89 bool kde = true;
90 #else
92 #ifdef __GNUC__
93 #warning "KDE Support disabled"
94 #endif
96 #endif
98 if (argc > 1)
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());
108 #ifndef QT_ONLY
109 printf("KDE: %s\n", KDE_VERSION_STRING);
110 #endif
111 return 0;
113 if ((arg == "--exec") || (arg == "-e"))
115 gui = false;
117 else if ((arg == "--interactive") || (arg == "-i"))
118 (*KJSEmbed::conout()) << "Interactive";
119 #ifndef QT_ONLY
120 else if ((arg == "-n") || (arg == "--no-kde"))
122 kde = false;
124 #endif
125 else
127 printUsage(appName);
128 return 0;
131 else
133 if (!script.isEmpty())
134 scriptArgs.append(KJS::jsString(arg));
135 else
136 script = arg;
140 else
142 printUsage(appName);
143 return 0;
146 // Setup QApplication
147 QCoreApplication *app;
149 #ifndef QT_ONLY
150 if (kde)
152 KAboutData aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2",
153 ki18n(""
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);
172 else
173 #endif
174 if (gui)
176 qDebug("no KDE");
177 app = new QApplication( argc, argv );
178 dynamic_cast<QApplication*>(app)->connect( app, SIGNAL( lastWindowClosed() ), SLOT(quit()) );
180 else
182 qDebug("no GUI");
183 app = new QCoreApplication(argc, argv);
185 qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed());
187 app->setApplicationName( appName );
189 // Setup Interpreter
190 time.restart();
191 Engine kernel;
192 qDebug(" New engine %dms", time.elapsed());
193 time.restart();
195 KJS::Interpreter *js = kernel.interpreter();
196 js->setShouldPrintExceptions(true);
197 KJS::ExecState *exec = js->globalExec();
199 // Publish bindings
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));
209 else // exec shell
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;
219 return (int)result;