summaryrefslogtreecommitdiffstats
path: root/kjsembed/qjscmd.cpp
blob: f8a95ce6dfe6ccd119f65de600eddc8ade9045b1 (plain)
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
/*
 *  Copyright (C) 2004, 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 <qstring.h>
#include <qregexp.h>
#include <qapplication.h>
#include <qmainwindow.h>

#include <kjs/interpreter.h>
#include <kjs/ustring.h>

#include "kjsembedpart.h"
#include "jsconsolewidget.h"
#include "jssecuritypolicy.h"
#include "kjsembed/global.h"

int main( int argc, char **argv )
{
#ifdef _WIN32
#   ifdef CONSOLEIO
    RedirectIOToConsole();
#   endif
#endif
    // Setup QApplication
    QApplication *app;
    app = new QApplication( argc, argv );
    app->connect( app, SIGNAL( lastWindowClosed() ), SLOT(quit()) );

    // 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 < argc ; i++ )
	l.append( KJS::String( argv[i] ) );

    KJS::Object argobj( js->builtinArray().construct( exec, l ) );
    appobj.put( exec, "args", argobj );

    if ( argc > 1 ) {
	// Run script
	bool ok = part->runFile( argv[1] );
	if ( !ok ) {
	    KJS::Completion jsres = part->completion();
	    (*KJSEmbed::conerr()) << jsres.value().toString(exec).qstring() << endl;
	    return 1;
	}
    }

    int result = 0;
    part->execute( QString("include('cmdline.js');") );

    return result;
}

// Local Variables:
// c-basic-offset: 4
// End: