1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
* Copyright (C) 2001-2003, Richard J. Moore <rich@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <errno.h>
#include <tqstring.h>
#include <tqregexp.h>
#include <tdeaboutdata.h>
#include <tdeapplication.h>
#include <tdecmdlineargs.h>
#include <kdebug.h>
#include <kimageio.h>
#include <tdelocale.h>
#include <tdemainwindow.h>
#include <kjs/interpreter.h>
#include <kjs/ustring.h>
#include "kjsembedpart.h"
#include "jsconsolewidget.h"
#include "jssecuritypolicy.h"
static TDECmdLineOptions options[] =
{
{ "c", 0, 0 },
{ "console", I18N_NOOP("Displays the KJSEmbed console"), 0 },
{ "i", 0, 0 },
{ "interactive", I18N_NOOP("Runs an interactive command line prompt"), 0 },
{ "exec", I18N_NOOP("Call application.exec() automatically after running the script"), 0 },
{ "nogui", I18N_NOOP("Disables all GUI facilities"), 0 },
{ "+[file]", I18N_NOOP("Script to execute, or '-' for stdin"), 0 },
{ 0, 0, 0 }
};
int main( int argc, char **argv )
{
bool nogui = false;
bool doexec = false;
TQCString appName = "kjscmd";
// Set the name of the instance
for ( int i = 1 ; i < argc ; i++ ) {
TQCString arg( argv[i] );
if ( arg[0] != '-' ) {
appName = arg.mid( arg.findRev('/') );
if ( !appName )
appName = argv[1];
else
argv[0] = appName.data();
}
}
// Clean app name
appName = appName.replace("/", "");
appName = appName.replace(".js", "");
// Search for nogui option
for ( int i = 1 ; i < argc ; i++ ) {
if ( strcmp( argv[i], "--exec" ) == 0 )
doexec = true;
if ( strcmp( argv[i], "--nogui" ) == 0 )
nogui = true;
}
// Cmd Line
TDEAboutData about( appName.data(), I18N_NOOP("KJSCmd"), KJSEMBED_VERSION_STRING,
I18N_NOOP("Runs KJS scripts from the command line."),
TDEAboutData::License_LGPL, I18N_NOOP("(c) 2001-2004 Richard Moore") );
about.addAuthor( "Richard Moore", 0, "rich@kde.org" );
about.addAuthor( "Ian Reinhart Geiser", 0, "geiser@kde.org" );
TDECmdLineArgs::init( argc, argv, &about );
TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.
TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
// Setup TDEApplication
TDEApplication *app;
if ( nogui ) {
app = new TDEApplication( false, false );
}
else {
app = new TDEApplication;
app->connect( app, TQT_SIGNAL( lastWindowClosed() ), TQT_SLOT(quit()) );
}
// Setup the rest
KImageIO::registerFormats();
// Setup Interpreter
KJSEmbed::JSSecurityPolicy::setDefaultPolicy( KJSEmbed::JSSecurityPolicy::CapabilityAll );
KJSEmbed::KJSEmbedPart *part = new KJSEmbed::KJSEmbedPart;
KJS::Interpreter *js = part->interpreter();
KJS::ExecState *exec = js->globalExec();
// Publish bindings
KJS::Object appobj = part->addObject( app, "application" );
// Build args array
KJS::List l;
for ( int i = 1 ; i < args->count() ; i++ )
l.append( KJS::String( args->arg(i) ) );
KJS::Object argobj( js->builtinArray().construct( exec, l ) );
appobj.put( exec, "args", argobj );
bool showConsole = false;
// Create Visible Console?
if ( args->isSet("console")
|| ( !args->count() && !args->isSet("interactive") ) ) {
if ( !nogui ) {
KJSEmbed::JSConsoleWidget *console = part->view();
console->resize( 600, 450 );
console->show();
part->addObject( console, "console" );
showConsole = true;
}
}
if ( args->count() ) {
// Run script
bool ok = part->runFile( args->arg(0) );
KJS::Completion jsres = part->completion();
if ( jsres.complType() != KJS::Normal) {
TQTextStream err( stderr, IO_WriteOnly );
TQTextStream out( stdout, IO_WriteOnly );
switch ( jsres.complType() ) {
case KJS::Break:
case KJS::Continue:
err << jsres.value().toString(exec).qstring() << endl;
return 1;
break;
case KJS::ReturnValue:
out << jsres.value().toString(exec).qstring() << endl;
return 0;
break;
case KJS::Throw:
{
KJS::Object exception = jsres.value().toObject(exec);
int line = exception.get(exec,KJS::Identifier("line")).toNumber(exec);
TQString type = exception.get(exec,KJS::Identifier("name")).toString(exec).qstring();
TQString message = exception.get(exec,KJS::Identifier("message")).toString(exec).qstring();
err << "Uncaught " << type << " exception at: " << line << endl;
err << message << endl;
return 1;
break;
}
default:
err << "Unknown error." << endl;
return 1;
break;
}
}
}
int result = 0;
if ( !nogui && ( doexec || showConsole ) )
result = app->exec();
if ( args->isSet( "interactive" ) ) {
part->execute( TQString("include('cmdline.js');") );
}
return result;
}
// Local Variables:
// c-basic-offset: 4
// End:
|