summaryrefslogtreecommitdiffstats
path: root/kjsembed/docs/examples/cmdline/cmdline.js
blob: 9c96c6cb67d0e4cebbb6e95f3547e6b326a33332 (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
#!/usr/bin/env kjscmd

function list(obj)
{
    println( obj );

    for ( var i in obj ) {
	try {
	    tp = typeof obj[i];

	    count = 20 - i.length;
	    spc = '';
	    for ( var s=0; s < count; s++ )
		spc = spc + ' ';

	    println( '   ' + i + spc + tp );
	}
	catch(err) {
	    println( '   ' + i + spc + 'Error, ' + err );
	}
    }
}

function help()
{
    println();
    println( 'KJSCmd ' + part.versionString + ' Prompt' );
    println( '======='        + '===' +       '=======' );
    println();
    println( "Type 'quit' to exit, and 'help' for this text. Anything else" );
    println( 'is evaluated by the KJSCmd engine and the result displayed.' );
    println();
}

function cmd_prompt()
{
    System.stdout.print( 'kjscmd> ' );
    return System.stdin.readLine();
}

//
// Main
//

println( "KJSCmd " + part.versionString + " (type 'help' for more information)" );
done = false;

do {

    line = cmd_prompt();

    if ( line == 'quit' ) {
	done = true;
    }
    else if ( line == 'help' ) {
	help();
    }
    else if ( line == '' ) {
	// DO NOTHING
    }
    else if ( line == null ) {
	done = true;
    }
    else {
	try {
	    output = eval( line );
	    
	    if ( typeof(output) != 'undefined' )
		println( output );
	}
	catch(err) {
	    println( 'Error: ' + err );
	}
    }

} while ( !done );

System.exit(0);