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