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