kjsembed
kjscmd.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QtGui/QApplication>
00025 #include <QtCore/QDebug>
00026 #include <QtCore/QStringList>
00027
00028 #ifndef QT_ONLY
00029 #include <kapplication.h>
00030 #include <kaboutdata.h>
00031 #include <kcmdlineargs.h>
00032 #endif // QT_ONLY
00033
00034 #include <kjs/interpreter.h>
00035 #include <kjs/ustring.h>
00036
00037 #include <kjsembed/kjseglobal.h>
00038 #include <kjsembed/kjsembed.h>
00039
00040 #include <QtCore/QDate>
00041
00042 using namespace KJSEmbed;
00043
00044 void printUsage(QString appName)
00045 {
00046 (*KJSEmbed::conerr()) << "Usage: " << appName << " [options] [file]" << endl
00047 << "Options:" << endl
00048 << " -e, --exec execute script without gui support." << endl
00049 << " -i, --interactive start interactive kjs interpreter." << endl
00050 #ifndef QT_ONLY
00051 << " -n, --no-kde start without KDE KApplication support." << endl
00052 #endif
00053 << endl;
00054 }
00055
00056 #ifndef QT_ONLY
00057
00058 #endif // QT_ONLY
00059
00060 int main( int argc, char **argv )
00061 {
00062 QTime time;
00063 time.start();
00064
00065 #ifdef _WIN32
00066 # ifdef CONSOLEIO
00067 RedirectIOToConsole();
00068 # endif
00069 #endif
00070
00071
00072 QString appName = argv[0];
00073 QStringList args;
00074 for (int i = 1; i < argc; i++ )
00075 {
00076 args << argv[i];
00077 }
00078
00079 QString script;
00080 KJS::List scriptArgs;
00081 bool gui = true;
00082 #ifndef QT_ONLY
00083
00084
00085
00086
00087
00088 bool kde = true;
00089 #else
00090
00091
00092
00093
00094
00095 #endif
00096
00097 if (argc > 1)
00098 {
00099 while (!args.isEmpty())
00100 {
00101 QString arg = args.takeFirst();
00102 if (arg.contains('-'))
00103 {
00104 if ((arg == "--exec") || (arg == "-e"))
00105 {
00106 gui = false;
00107 }
00108 else if ((arg == "--interactive") || (arg == "-i"))
00109 (*KJSEmbed::conout()) << "Interactive";
00110 #ifndef QT_ONLY
00111 else if ((arg == "-n") || (arg == "--no-kde"))
00112 {
00113 kde = false;
00114 }
00115 #endif
00116 else
00117 {
00118 printUsage(appName);
00119 return 0;
00120 }
00121 }
00122 else
00123 {
00124 if (!script.isEmpty())
00125 scriptArgs.append(KJS::jsString(arg));
00126 else
00127 script = arg;
00128 }
00129 }
00130 }
00131 else
00132 {
00133 printUsage(appName);
00134 return 0;
00135 }
00136
00137
00138 QCoreApplication *app;
00139
00140 #ifndef QT_ONLY
00141 if (kde)
00142 {
00143 KAboutData aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2",
00144 ki18n(""
00145 "Utility for running KJSEmbed scripts \n" ),
00146 KAboutData::License_LGPL,
00147 ki18n("(C) 2005-2006 The KJSEmbed Authors") );
00148
00149 KCmdLineOptions options;
00150 options.add("e", ki18n("Execute script without gui support"));
00151 options.add("exec", ki18n("Execute script without gui support"));
00152 options.add("i", ki18n("start interactive kjs interpreter"));
00153 options.add("interactive", ki18n("start interactive kjs interpreter"));
00154 options.add("n", ki18n("start without KDE KApplication support."));
00155 options.add("no-kde", ki18n("start without KDE KApplication support."));
00156 options.add("!+command", ki18n("Script to execute"));
00157
00158 KCmdLineArgs::addCmdLineOptions( options );
00159 KCmdLineArgs::init( argc, argv, &aboutData );
00160
00161 app = new KApplication(gui);
00162 }
00163 else
00164 #endif
00165 if (gui)
00166 {
00167 qDebug("no KDE");
00168 app = new QApplication( argc, argv );
00169 dynamic_cast<QApplication*>(app)->connect( app, SIGNAL( lastWindowClosed() ), SLOT(quit()) );
00170 }
00171 else
00172 {
00173 qDebug("no GUI");
00174 app = new QCoreApplication(argc, argv);
00175 }
00176 qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed());
00177
00178 app->setApplicationName( appName );
00179
00180
00181 time.restart();
00182 Engine kernel;
00183 qDebug(" New engine %dms", time.elapsed());
00184 time.restart();
00185
00186 KJS::Interpreter *js = kernel.interpreter();
00187 js->setShouldPrintExceptions(true);
00188 KJS::ExecState *exec = js->globalExec();
00189
00190
00191 KJS::JSObject *appObject = kernel.addObject( app, "Application" );
00192 KJS::JSObject *argObject = js->builtinArray()->construct( exec, scriptArgs );
00193 appObject->put( exec, "args", argObject );
00194 Engine::ExitStatus result = Engine::Failure;
00195
00196 if (!script.isEmpty())
00197 {
00198 result = kernel.runFile(toUString(script));
00199 }
00200 else
00201 {
00202 result = kernel.runFile( ":/console.js" );
00203 }
00204
00205 if ( result != Engine::Success )
00206 {
00207 KJS::Completion jsres = kernel.completion();
00208 (*KJSEmbed::conerr()) << toQString(jsres.value()->toString(exec)) << endl;
00209 }
00210 return (int)result;
00211 }
00212