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 <kdebug.h>
00030 #include <kinstance.h>
00031 #include <kapplication.h>
00032 #include <kcmdlineargs.h>
00033 #include <kaboutdata.h>
00034 #include <ksharedptr.h>
00035 
00036 // Kross
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     // Read the scriptfile
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     // Determinate the matching interpreter
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     // Run the script.
00075     try {
00076         // First we need a scriptcontainer and fill it.
00077         Kross::Api::ScriptContainer::Ptr scriptcontainer = manager->getScriptContainer(scriptfile);
00078         scriptcontainer->setInterpreterName( interpreterinfo->getInterpretername() );
00079         scriptcontainer->setCode(scriptcode);
00080         // Now execute the scriptcontainer.
00081         scriptcontainer->execute();
00082         if(scriptcontainer->hadException()) {
00083             // We had an exception.
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         // Normaly that shouldn't be the case...
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     // Initialize command line args
00114     KCmdLineArgs::init(argc, argv, &about);
00115     // Tell which options are supported and parse them.
00116     static KCmdLineOptions options[] = {
00117         { "+file", I18N_NOOP("Scriptfile"), 0 },
00118         KCmdLineLastOption
00119     };
00120     KCmdLineArgs::addCmdLineOptions(options);
00121     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00122 
00123     // If no options are defined.
00124     if(args->count() < 1) {
00125         std::cout << "Syntax: " << KCmdLineArgs::appName() << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
00126         return ERROR_HELP;
00127     }
00128 
00129     // Create KApplication instance.
00130     app = new KApplication( /* allowStyles */ true, /* GUIenabled */ true );
00131 
00132     //QString interpretername = args->getOption("interpreter");
00133     //QString scriptfilename = args->getOption("scriptfile");
00134 
00135     // Each argument is a scriptfile to open
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     // Free the KApplication instance and exit the program.
00143     delete app;
00144     return result;
00145 }
KDE Home | KDE Accessibility Home | Description of Access Keys