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