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 #ifdef __GNUC__
00084 #warning "KDE Support enabled"
00085 #endif
00086 bool kde = true;
00087 #else
00088 #ifdef __GNUC__
00089 #warning "KDE Support disabled"
00090 #endif
00091 #endif
00092
00093 if (argc > 1)
00094 {
00095 while (!args.isEmpty())
00096 {
00097 QString arg = args.takeFirst();
00098 if (arg.contains('-'))
00099 {
00100 if ((arg == "--exec") || (arg == "-e"))
00101 {
00102 gui = false;
00103 }
00104 else if ((arg == "--interactive") || (arg == "-i"))
00105 (*KJSEmbed::conout()) << "Interactive";
00106 #ifndef QT_ONLY
00107 else if ((arg == "-n") || (arg == "--no-kde"))
00108 {
00109 kde = false;
00110 }
00111 #endif
00112 else
00113 {
00114 printUsage(appName);
00115 return 0;
00116 }
00117 }
00118 else
00119 {
00120 if (!script.isEmpty())
00121 scriptArgs.append(KJS::jsString(arg));
00122 else
00123 script = arg;
00124 }
00125 }
00126 }
00127 else
00128 {
00129 printUsage(appName);
00130 return 0;
00131 }
00132
00133
00134 QCoreApplication *app;
00135
00136 #ifndef QT_ONLY
00137 if (kde)
00138 {
00139 KAboutData aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2",
00140 ki18n(""
00141 "Utility for running KJSEmbed scripts \n" ),
00142 KAboutData::License_LGPL,
00143 ki18n("(C) 2005-2006 The KJSEmbed Authors") );
00144
00145 KCmdLineOptions options;
00146 options.add("e", ki18n("Execute script without gui support"));
00147 options.add("exec", ki18n("Execute script without gui support"));
00148 options.add("i", ki18n("start interactive kjs interpreter"));
00149 options.add("interactive", ki18n("start interactive kjs interpreter"));
00150 options.add("n", ki18n("start without KDE KApplication support."));
00151 options.add("no-kde", ki18n("start without KDE KApplication support."));
00152 options.add("!+command", ki18n("Script to execute"));
00153
00154 KCmdLineArgs::addCmdLineOptions( options );
00155 KCmdLineArgs::init( argc, argv, &aboutData );
00156
00157 app = new KApplication(gui);
00158 }
00159 else
00160 #endif
00161 if (gui)
00162 {
00163 qDebug("no KDE");
00164 app = new QApplication( argc, argv );
00165 dynamic_cast<QApplication*>(app)->connect( app, SIGNAL( lastWindowClosed() ), SLOT(quit()) );
00166 }
00167 else
00168 {
00169 qDebug("no GUI");
00170 app = new QCoreApplication(argc, argv);
00171 }
00172 qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed());
00173
00174 app->setApplicationName( appName );
00175
00176
00177 time.restart();
00178 Engine kernel;
00179 qDebug(" New engine %dms", time.elapsed());
00180 time.restart();
00181
00182 KJS::Interpreter *js = kernel.interpreter();
00183 js->setShouldPrintExceptions(true);
00184 KJS::ExecState *exec = js->globalExec();
00185
00186
00187 KJS::JSObject *appObject = kernel.addObject( app, "Application" );
00188 KJS::JSObject *argObject = js->builtinArray()->construct( exec, scriptArgs );
00189 appObject->put( exec, "args", argObject );
00190 Engine::ExitStatus result = Engine::Failure;
00191
00192 if (!script.isEmpty())
00193 {
00194 result = kernel.runFile(toUString(script));
00195 }
00196 else
00197 {
00198 result = kernel.runFile( ":/console.js" );
00199 }
00200
00201 if ( result != Engine::Success )
00202 {
00203 KJS::Completion jsres = kernel.completion();
00204 (*KJSEmbed::conerr()) << toQString(jsres.value()->toString(exec)) << endl;
00205 }
00206 return (int)result;
00207 }
00208