lib

main.cpp

00001 /***************************************************************************
00002  * main.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2006 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 // for std namespace
00021 #include <string>
00022 #include <iostream>
00023 
00024 // Qt
00025 #include <qstring.h>
00026 #include <qfile.h>
00027 
00028 // KDE
00029 #include <kinstance.h>
00030 #include <kapplication.h>
00031 #include <kcmdlineargs.h>
00032 #include <kaboutdata.h>
00033 #include <ksharedptr.h>
00034 
00035 // Kross
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     // Read the scriptfile
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     // Determinate the matching interpreter
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     // Run the script.
00074     try {
00075         // First we need a scriptcontainer and fill it.
00076         Kross::Api::ScriptContainer::Ptr scriptcontainer = manager->getScriptContainer(scriptfile);
00077         scriptcontainer->setInterpreterName( interpreterinfo->getInterpretername() );
00078         scriptcontainer->setCode(scriptcode);
00079         // Now execute the scriptcontainer.
00080         scriptcontainer->execute();
00081         if(scriptcontainer->hadException()) {
00082             // We had an exception.
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         // Normaly that shouldn't be the case...
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     // Initialize command line args
00113     KCmdLineArgs::init(argc, argv, &about);
00114     // Tell which options are supported and parse them.
00115     static KCmdLineOptions options[] = {
00116         { "+file", I18N_NOOP("Scriptfile"), 0 },
00117         KCmdLineLastOption
00118     };
00119     KCmdLineArgs::addCmdLineOptions(options);
00120     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00121 
00122     // If no options are defined.
00123     if(args->count() < 1) {
00124         std::cout << "Syntax: " << KCmdLineArgs::appName() << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
00125         return ERROR_HELP;
00126     }
00127 
00128     // Create KApplication instance.
00129     app = new KApplication( /* allowStyles */ true, /* GUIenabled */ true );
00130 
00131     //QString interpretername = args->getOption("interpreter");
00132     //QString scriptfilename = args->getOption("scriptfile");
00133 
00134     // Each argument is a scriptfile to open
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     // Free the KApplication instance and exit the program.
00142     delete app;
00143     return result;
00144 }
KDE Home | KDE Accessibility Home | Description of Access Keys