kross/runner/main.cpp
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 <string>
22#include <iostream>
23
24// TQt
25#include <tqstring.h>
26#include <tqfile.h>
27
28// KDE
29#include <tdeinstance.h>
30#include <tdeapplication.h>
31#include <tdecmdlineargs.h>
32#include <tdeaboutdata.h>
33#include <ksharedptr.h>
34
35// Kross
36#include "../main/manager.h"
37#include "../main/scriptcontainer.h"
38#include "../api/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_UNHALDEDEXCEPTION -5
46#define ERROR_EXCEPTION -6
47
48TDEApplication* app = 0;
49
50int runScriptFile(const TQString& scriptfile)
51{
52 // Read the scriptfile
53 TQFile f(TQFile::encodeName(scriptfile));
54 if(! f.exists()) {
55 std::cerr << "No such scriptfile: " << scriptfile.latin1() << std::endl;
56 return ERROR_NOSUCHFILE;
57 }
58 if(! f.open(IO_ReadOnly)) {
59 std::cerr << "Failed to open scriptfile: " << scriptfile.latin1() << std::endl;
60 return ERROR_OPENFAILED;
61 }
62 TQString scriptcode = f.readAll();
63 f.close();
64
65 // Determinate the matching interpreter
67 Kross::Api::InterpreterInfo* interpreterinfo = manager->getInterpreterInfo( manager->getInterpreternameForFile(scriptfile) );
68 if(! interpreterinfo) {
69 std::cerr << "No interpreter for file: " << scriptfile.latin1() << std::endl;
70 return ERROR_NOINTERPRETER;
71 }
72
73 // Run the script.
74 try {
75 // First we need a scriptcontainer and fill it.
76 Kross::Api::ScriptContainer::Ptr scriptcontainer = manager->getScriptContainer(scriptfile);
77 scriptcontainer->setInterpreterName( interpreterinfo->getInterpretername() );
78 scriptcontainer->setCode(scriptcode);
79 // Now execute the scriptcontainer.
80 scriptcontainer->execute();
81 if(scriptcontainer->hadException()) {
82 // We had an exception.
83 TQString errormessage = scriptcontainer->getException()->getError();
84 TQString tracedetails = scriptcontainer->getException()->getTrace();
85 std::cerr << TQString("%2\n%1").arg(tracedetails).arg(errormessage).latin1() << std::endl;
86 return ERROR_EXCEPTION;
87 }
88 }
90 // Normaly that shouldn't be the case...
91 std::cerr << TQString("EXCEPTION %1").arg(e->toString()).latin1() << std::endl;
92 return ERROR_UNHALDEDEXCEPTION;
93 }
94 return ERROR_OK;
95}
96
97int main(int argc, char **argv)
98{
99 int result = ERROR_OK;
100
101 TDEAboutData about("krossrunner",
102 "krossrunner",
103 "0.1",
104 "KDE application to run Kross scripts.",
105 TDEAboutData::License_LGPL,
106 "(C) 2006 Sebastian Sauer",
107 "Run Kross scripts.",
108 "http://www.dipe.org/kross",
109 "kross@dipe.org");
110 about.addAuthor("Sebastian Sauer", "Author", "mail@dipe.org");
111
112 // Initialize command line args
113 TDECmdLineArgs::init(argc, argv, &about);
114 // Tell which options are supported and parse them.
115 static TDECmdLineOptions options[] = {
116 { "+file", I18N_NOOP("Scriptfile"), 0 },
117 TDECmdLineLastOption
118 };
119 TDECmdLineArgs::addCmdLineOptions(options);
120 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
121
122 // If no options are defined.
123 if(args->count() < 1) {
124 std::cout << "Syntax: " << TDECmdLineArgs::appName() << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
125 return ERROR_HELP;
126 }
127
128 // Create TDEApplication instance.
129 app = new TDEApplication( /* allowStyles */ true, /* GUIenabled */ true );
130
131 //TQString interpretername = args->getOption("interpreter");
132 //TQString scriptfilename = args->getOption("scriptfile");
133
134 // Each argument is a scriptfile to open
135 for(int i = 0; i < args->count(); i++) {
136 result = runScriptFile(TQFile::decodeName(args->arg(i)));
137 if(result != ERROR_OK)
138 break;
139 }
140
141 // Free the TDEApplication instance and exit the program.
142 delete app;
143 return result;
144}
TDESharedPtr< Exception > Ptr
Shared pointer to implement reference-counting.
Definition exception.h:45
While the Interpreter is the implemented interpreter this class is used to provide some abstract info...
Definition interpreter.h:42
const TQString getInterpretername()
The Manager class is the main entry point to work with Kross.
Definition manager.h:55
const TQString getInterpreternameForFile(const TQString &file)
Definition manager.cpp:142
InterpreterInfo * getInterpreterInfo(const TQString &interpretername)
Definition manager.cpp:137
static Manager * scriptManager()
Return the Manager instance.
Definition manager.cpp:67
TDESharedPtr< ScriptContainer > getScriptContainer(const TQString &scriptname)
Return the existing ScriptContainer with scriptname or create a new ScriptContainer instance and asso...
Definition manager.cpp:154
TDESharedPtr< ScriptContainer > Ptr
Shared pointer to implement reference-counting.