lib
main.cpp00001
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.h>
00026 #include <qfile.h>
00027
00028
00029 #include <kdebug.h>
00030 #include <kinstance.h>
00031 #include <kapplication.h>
00032 #include <kcmdlineargs.h>
00033 #include <kaboutdata.h>
00034 #include <ksharedptr.h>
00035
00036
00037 #include "../main/manager.h"
00038 #include "../main/scriptcontainer.h"
00039 #include "../api/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_UNHALDEDEXCEPTION -5
00047 #define ERROR_EXCEPTION -6
00048
00049 KApplication* app = 0;
00050
00051 int runScriptFile(const QString& scriptfile)
00052 {
00053
00054 QFile f(QFile::encodeName(scriptfile));
00055 if(! f.exists()) {
00056 std::cerr << "No such scriptfile: " << scriptfile.latin1() << std::endl;
00057 return ERROR_NOSUCHFILE;
00058 }
00059 if(! f.open(IO_ReadOnly)) {
00060 std::cerr << "Failed to open scriptfile: " << scriptfile.latin1() << std::endl;
00061 return ERROR_OPENFAILED;
00062 }
00063 QString scriptcode = f.readAll();
00064 f.close();
00065
00066
00067 Kross::Api::Manager* manager = Kross::Api::Manager::scriptManager();
00068 Kross::Api::InterpreterInfo* interpreterinfo = manager->getInterpreterInfo( manager->getInterpreternameForFile(scriptfile) );
00069 if(! interpreterinfo) {
00070 std::cerr << "No interpreter for file: " << scriptfile.latin1() << std::endl;
00071 return ERROR_NOINTERPRETER;
00072 }
00073
00074
00075 try {
00076
00077 Kross::Api::ScriptContainer::Ptr scriptcontainer = manager->getScriptContainer(scriptfile);
00078 scriptcontainer->setInterpreterName( interpreterinfo->getInterpretername() );
00079 scriptcontainer->setCode(scriptcode);
00080
00081 scriptcontainer->execute();
00082 if(scriptcontainer->hadException()) {
00083
00084 QString errormessage = scriptcontainer->getException()->getError();
00085 QString tracedetails = scriptcontainer->getException()->getTrace();
00086 std::cerr << QString("%2\n%1").arg(tracedetails).arg(errormessage).latin1() << std::endl;
00087 return ERROR_EXCEPTION;
00088 }
00089 }
00090 catch(Kross::Api::Exception::Ptr e) {
00091
00092 std::cerr << QString("EXCEPTION %1").arg(e->toString()).latin1() << std::endl;
00093 return ERROR_UNHALDEDEXCEPTION;
00094 }
00095 return ERROR_OK;
00096 }
00097
00098 int main(int argc, char **argv)
00099 {
00100 int result = ERROR_OK;
00101
00102 KAboutData about("krossrunner",
00103 "krossrunner",
00104 "0.1",
00105 "KDE application to run Kross scripts.",
00106 KAboutData::License_LGPL,
00107 "(C) 2006 Sebastian Sauer",
00108 "Run Kross scripts.",
00109 "http://www.dipe.org/kross",
00110 "kross@dipe.org");
00111 about.addAuthor("Sebastian Sauer", "Author", "mail@dipe.org");
00112
00113
00114 KCmdLineArgs::init(argc, argv, &about);
00115
00116 static KCmdLineOptions options[] = {
00117 { "+file", I18N_NOOP("Scriptfile"), 0 },
00118 KCmdLineLastOption
00119 };
00120 KCmdLineArgs::addCmdLineOptions(options);
00121 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00122
00123
00124 if(args->count() < 1) {
00125 std::cout << "Syntax: " << KCmdLineArgs::appName() << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
00126 return ERROR_HELP;
00127 }
00128
00129
00130 app = new KApplication( true, true );
00131
00132
00133
00134
00135
00136 for(int i = 0; i < args->count(); i++) {
00137 result = runScriptFile(QFile::decodeName(args->arg(i)));
00138 if(result != ERROR_OK)
00139 break;
00140 }
00141
00142
00143 delete app;
00144 return result;
00145 }
|