1 /***************************************************************************
2  * main.cpp
3  * This file is part of the KDE project
4  * copyright (C)2006 by Sebastian Sauer (mail@dipe.org)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  ***************************************************************************/
19 
20 // for std namespace
21 #include <iostream>
22 
23 // Qt
24 
25 #include <QFile>
26 #include <QApplication>
27 #include <QUrl>
28 
29 // KDE
30 #include <KAboutData>
31 #include <klocalizedstring.h>
32 #include <qcommandlineparser.h>
33 #include <qcommandlineoption.h>
34 
35 // Kross
36 #include "../core/manager.h"
37 #include "../core/action.h"
38 #include "../core/interpreter.h"
39 
40 #define ERROR_OK 0
41 #define ERROR_HELP -1
42 #define ERROR_NOSUCHFILE -2
43 #define ERROR_OPENFAILED -3
44 #define ERROR_NOINTERPRETER -4
45 #define ERROR_EXCEPTION -6
46 
47 QApplication *app = nullptr;
48 
runScriptFile(const QString & scriptfile)49 int runScriptFile(const QString &scriptfile)
50 {
51     // Read the scriptfile
52     QFile f(scriptfile);
53     if (! f.exists()) {
54         std::cerr << "No such scriptfile: " << scriptfile.toLatin1().data() << std::endl;
55         return ERROR_NOSUCHFILE;
56     }
57     if (! f.open(QIODevice::ReadOnly)) {
58         std::cerr << "Failed to open scriptfile: " << scriptfile.toLatin1().data() << std::endl;
59         return ERROR_OPENFAILED;
60     }
61     QByteArray scriptcode = f.readAll();
62     f.close();
63 
64     // Determinate the matching interpreter
65     Kross::InterpreterInfo *interpreterinfo = Kross::Manager::self().interpreterInfo(Kross::Manager::self().interpreternameForFile(scriptfile));
66     if (! interpreterinfo) {
67         std::cerr << "No interpreter for file: " << scriptfile.toLatin1().data() << std::endl;
68         return ERROR_NOINTERPRETER;
69     }
70 
71     // First we need a Action and fill it.
72     Kross::Action *action = new Kross::Action(nullptr /*no parent*/, QUrl::fromUserInput(scriptfile));
73     action->setInterpreter(interpreterinfo->interpreterName());
74     action->setCode(scriptcode);
75 
76     // Now execute the Action.
77     action->trigger();
78 
79     if (action->hadError()) {
80         // We had an exception.
81         std::cerr << QString("%2\n%1").arg(action->errorTrace()).arg(action->errorMessage()).toLatin1().data() << std::endl;
82         delete action;
83         return ERROR_EXCEPTION;
84     }
85 
86     delete action;
87     return ERROR_OK;
88 }
89 
main(int argc,char ** argv)90 int main(int argc, char **argv)
91 {
92     app = new QApplication(argc, argv);
93 
94     int result = ERROR_OK;
95 
96     KLocalizedString::setApplicationDomain("kross5");
97 
98     KAboutData about(QStringLiteral("kross"),
99                      i18nc("application name", "Kross"),
100                      QStringLiteral("0.1"),
101                      i18nc("application description", "Command-line utility to run Kross scripts."),
102                      KAboutLicense::LGPL,
103                      i18nc("@info:credit", "Copyright 2006 Sebastian Sauer"),
104                      QString(),
105                      QStringLiteral("http://kross.dipe.org"),
106                      QStringLiteral("kross@dipe.org"));
107     about.addAuthor(i18nc("@info:credit", "Sebastian Sauer"),
108                     i18nc("@info:credit", "Author"),
109                     QStringLiteral("mail@dipe.org"));
110     KAboutData::setApplicationData(about);
111 
112     // Initialize command line args
113     // Tell which options are supported and parse them.
114     QCommandLineParser parser;
115     about.setupCommandLine(&parser);
116     parser.addPositionalArgument("file", i18nc("@info:shell command-line argument",
117                                                "The script to run."));
118 
119     parser.process(*app);
120     about.processCommandLine(&parser);
121 
122     const QStringList args = parser.positionalArguments();
123     // If no options are defined.
124     if (args.count() < 1) {
125         parser.showHelp();
126         //std::cout << "Syntax: " << "kross" << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
127         return ERROR_HELP;
128     }
129 
130     // Each argument is a scriptfile to open
131     for (int i = 0; i < args.count(); i++) {
132         result = runScriptFile(args.at(i));
133         if (result != ERROR_OK) {
134             break;
135         }
136     }
137 
138     // Free the QApplication instance and exit the program.
139     delete app;
140     Kross::Manager::self().deleteModules();
141     return result;
142 }
143