File indexing completed on 2024-04-14 14:27:08

0001 /***************************************************************************
0002  * main.cpp
0003  * This file is part of the KDE project
0004  * copyright (C)2006 by Sebastian Sauer (mail@dipe.org)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 // for std namespace
0021 #include <iostream>
0022 
0023 // Qt
0024 
0025 #include <QFile>
0026 #include <QApplication>
0027 #include <QUrl>
0028 
0029 // KDE
0030 #include <KAboutData>
0031 #include <klocalizedstring.h>
0032 #include <qcommandlineparser.h>
0033 #include <qcommandlineoption.h>
0034 
0035 // Kross
0036 #include "../core/manager.h"
0037 #include "../core/action.h"
0038 #include "../core/interpreter.h"
0039 
0040 #define ERROR_OK 0
0041 #define ERROR_HELP -1
0042 #define ERROR_NOSUCHFILE -2
0043 #define ERROR_OPENFAILED -3
0044 #define ERROR_NOINTERPRETER -4
0045 #define ERROR_EXCEPTION -6
0046 
0047 QApplication *app = nullptr;
0048 
0049 int runScriptFile(const QString &scriptfile)
0050 {
0051     // Read the scriptfile
0052     QFile f(scriptfile);
0053     if (! f.exists()) {
0054         std::cerr << "No such scriptfile: " << scriptfile.toLatin1().data() << std::endl;
0055         return ERROR_NOSUCHFILE;
0056     }
0057     if (! f.open(QIODevice::ReadOnly)) {
0058         std::cerr << "Failed to open scriptfile: " << scriptfile.toLatin1().data() << std::endl;
0059         return ERROR_OPENFAILED;
0060     }
0061     QByteArray scriptcode = f.readAll();
0062     f.close();
0063 
0064     // Determinate the matching interpreter
0065     Kross::InterpreterInfo *interpreterinfo = Kross::Manager::self().interpreterInfo(Kross::Manager::self().interpreternameForFile(scriptfile));
0066     if (! interpreterinfo) {
0067         std::cerr << "No interpreter for file: " << scriptfile.toLatin1().data() << std::endl;
0068         return ERROR_NOINTERPRETER;
0069     }
0070 
0071     // First we need a Action and fill it.
0072     Kross::Action *action = new Kross::Action(nullptr /*no parent*/, QUrl::fromUserInput(scriptfile));
0073     action->setInterpreter(interpreterinfo->interpreterName());
0074     action->setCode(scriptcode);
0075 
0076     // Now execute the Action.
0077     action->trigger();
0078 
0079     if (action->hadError()) {
0080         // We had an exception.
0081         std::cerr << QString("%2\n%1").arg(action->errorTrace()).arg(action->errorMessage()).toLatin1().data() << std::endl;
0082         delete action;
0083         return ERROR_EXCEPTION;
0084     }
0085 
0086     delete action;
0087     return ERROR_OK;
0088 }
0089 
0090 int main(int argc, char **argv)
0091 {
0092     app = new QApplication(argc, argv);
0093 
0094     int result = ERROR_OK;
0095 
0096     KLocalizedString::setApplicationDomain("kross5");
0097 
0098     KAboutData about(QStringLiteral("kross"),
0099                      i18nc("application name", "Kross"),
0100                      QStringLiteral("0.1"),
0101                      i18nc("application description", "Command-line utility to run Kross scripts."),
0102                      KAboutLicense::LGPL,
0103                      i18nc("@info:credit", "Copyright 2006 Sebastian Sauer"),
0104                      QString(),
0105                      QStringLiteral("http://kross.dipe.org"),
0106                      QStringLiteral("kross@dipe.org"));
0107     about.addAuthor(i18nc("@info:credit", "Sebastian Sauer"),
0108                     i18nc("@info:credit", "Author"),
0109                     QStringLiteral("mail@dipe.org"));
0110     KAboutData::setApplicationData(about);
0111 
0112     // Initialize command line args
0113     // Tell which options are supported and parse them.
0114     QCommandLineParser parser;
0115     about.setupCommandLine(&parser);
0116     parser.addPositionalArgument("file", i18nc("@info:shell command-line argument",
0117                                                "The script to run."));
0118 
0119     parser.process(*app);
0120     about.processCommandLine(&parser);
0121 
0122     const QStringList args = parser.positionalArguments();
0123     // If no options are defined.
0124     if (args.count() < 1) {
0125         parser.showHelp();
0126         //std::cout << "Syntax: " << "kross" << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
0127         return ERROR_HELP;
0128     }
0129 
0130     // Each argument is a scriptfile to open
0131     for (int i = 0; i < args.count(); i++) {
0132         result = runScriptFile(args.at(i));
0133         if (result != ERROR_OK) {
0134             break;
0135         }
0136     }
0137 
0138     // Free the QApplication instance and exit the program.
0139     delete app;
0140     Kross::Manager::self().deleteModules();
0141     return result;
0142 }