01: /*
02: * $Id: Console.java,v 1.7 2006/12/22 14:06:40 thiago Exp $
03: * Copyright (C) 2003-2007 Kepler Project.
04: *
05: * Permission is hereby granted, free of charge, to any person obtaining
06: * a copy of this software and associated documentation files (the
07: * "Software"), to deal in the Software without restriction, including
08: * without limitation the rights to use, copy, modify, merge, publish,
09: * distribute, sublicense, and/or sell copies of the Software, and to
10: * permit persons to whom the Software is furnished to do so, subject to
11: * the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be
14: * included in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23: */
24:
25: package org.keplerproject.luajava;
26:
27: import java.io.BufferedReader;
28: import java.io.InputStreamReader;
29:
30: /**
31: * Simple LuaJava console.
32: * This is also an example on how to use the Java side of LuaJava and how to startup
33: * a LuaJava application.
34: *
35: * @author Thiago Ponte
36: */
37: public class Console {
38:
39: /**
40: * Creates a console for user interaction.
41: *
42: * @param args names of the lua files to be executed
43: */
44: public static void main(String[] args) {
45: try {
46: LuaState L = LuaStateFactory.newLuaState();
47: L.openLibs();
48:
49: if (args.length > 0) {
50: for (int i = 0; i < args.length; i++) {
51: int res = L.LloadFile(args[i]);
52: if (res == 0) {
53: res = L.pcall(0, 0, 0);
54: }
55: if (res != 0) {
56: throw new LuaException("Error on file: "
57: + args[i] + ". " + L.toString(-1));
58: }
59: }
60:
61: return;
62: }
63:
64: System.out.println("API Lua Java - console mode.");
65:
66: BufferedReader inp = new BufferedReader(
67: new InputStreamReader(System.in));
68:
69: String line;
70:
71: System.out.print("> ");
72: while ((line = inp.readLine()) != null
73: && !line.equals("exit")) {
74: int ret = L
75: .LloadBuffer(line.getBytes(), "from console");
76: if (ret == 0) {
77: ret = L.pcall(0, 0, 0);
78: }
79: if (ret != 0) {
80: System.err.println("Error on line: " + line);
81: System.err.println(L.toString(-1));
82: }
83: System.out.print("> ");
84: }
85:
86: L.close();
87: } catch (Exception e) {
88: e.printStackTrace();
89: }
90:
91: }
92: }
|