Kross
main.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 #include <iostream>
00022
00023
00024
00025 #include <QtCore/QFile>
00026
00027
00028 #include <kapplication.h>
00029 #include <kcmdlineargs.h>
00030 #include <kaboutdata.h>
00031 #include <kurl.h>
00032
00033
00034 #include "../core/manager.h"
00035 #include "../core/action.h"
00036 #include "../core/interpreter.h"
00037
00038 #define ERROR_OK 0
00039 #define ERROR_HELP -1
00040 #define ERROR_NOSUCHFILE -2
00041 #define ERROR_OPENFAILED -3
00042 #define ERROR_NOINTERPRETER -4
00043 #define ERROR_EXCEPTION -6
00044
00045 KApplication* app = 0;
00046
00047 int runScriptFile(const QString& scriptfile)
00048 {
00049
00050 QFile f(scriptfile);
00051 if(! f.exists()) {
00052 std::cerr << "No such scriptfile: " << scriptfile.toLatin1().data() << std::endl;
00053 return ERROR_NOSUCHFILE;
00054 }
00055 if(! f.open(QIODevice::ReadOnly)) {
00056 std::cerr << "Failed to open scriptfile: " << scriptfile.toLatin1().data() << std::endl;
00057 return ERROR_OPENFAILED;
00058 }
00059 QByteArray scriptcode = f.readAll();
00060 f.close();
00061
00062
00063 Kross::InterpreterInfo* interpreterinfo = Kross::Manager::self().interpreterInfo( Kross::Manager::self().interpreternameForFile(scriptfile) );
00064 if(! interpreterinfo) {
00065 std::cerr << "No interpreter for file: " << scriptfile.toLatin1().data() << std::endl;
00066 return ERROR_NOINTERPRETER;
00067 }
00068
00069
00070 Kross::Action* action = new Kross::Action(0 , KUrl(scriptfile));
00071 action->setInterpreter( interpreterinfo->interpreterName() );
00072 action->setCode(scriptcode);
00073
00074
00075 action->trigger();
00076
00077 if(action->hadError()) {
00078
00079 std::cerr << QString("%2\n%1").arg(action->errorTrace()).arg(action->errorMessage()).toLatin1().data() << std::endl;
00080 delete action;
00081 return ERROR_EXCEPTION;
00082 }
00083
00084 delete action;
00085 return ERROR_OK;
00086 }
00087
00088 int main(int argc, char **argv)
00089 {
00090 int result = ERROR_OK;
00091
00092 KAboutData about("kross",
00093 "kdelibs4",
00094 ki18n("Kross"),
00095 "0.1",
00096 ki18n("KDE application to run Kross scripts."),
00097 KAboutData::License_LGPL,
00098 ki18n("(C) 2006 Sebastian Sauer"),
00099 ki18n("Run Kross scripts."),
00100 "http://kross.dipe.org",
00101 "kross@dipe.org");
00102 about.addAuthor(ki18n("Sebastian Sauer"), ki18n("Author"), "mail@dipe.org");
00103
00104
00105 KCmdLineArgs::init(argc, argv, &about);
00106
00107 KCmdLineOptions options;
00108 options.add("+file", ki18n("Scriptfile"));
00109
00110 KCmdLineArgs::addCmdLineOptions(options);
00111 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00112
00113
00114 if(args->count() < 1) {
00115 std::cout << "Syntax: " << KCmdLineArgs::appName().toLocal8Bit().constData() << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
00116 return ERROR_HELP;
00117 }
00118
00119
00120 app = new KApplication( true );
00121
00122
00123 for(int i = 0; i < args->count(); i++) {
00124 result = runScriptFile(args->arg(i));
00125 if(result != ERROR_OK)
00126 break;
00127 }
00128
00129
00130 delete app;
00131 return result;
00132 }