reorganized file layout (part 1: moves and renames)
[org.ibex.core.git] / src / org / ibex / core / Main.java
diff --git a/src/org/ibex/core/Main.java b/src/org/ibex/core/Main.java
new file mode 100644 (file)
index 0000000..8003ada
--- /dev/null
@@ -0,0 +1,103 @@
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.ibex;
+
+import java.net.*;
+import java.io.*;
+import java.util.*;
+import org.ibex.js.*;
+import org.ibex.util.*;
+
+/** Entry point for the Ibex Engine; handles splash screen, initial xwar loading, and argument processing */
+public class Main {
+
+    /**
+     *  FEATURE: this should be implemented using self-emulation
+     *  Used for security checks. If this is null, it means that only
+     *  scripts originating from the local filesystem are loaded in
+     *  the engine (maximum permissions). If scripts have been loaded
+     *  from multiple locations, this will be 0.0.0.0 (the invalid
+     *  IP).
+     */
+    public static java.net.InetAddress originAddr = null;
+    public static String originHost = null;
+    public static String origin = null;
+    public static String initialTemplate = null;
+    
+    public static final Stream builtin = new Stream.Zip(new Stream.Builtin());
+    public static Picture scarImage = null;
+
+    public static void printUsage() {
+        System.err.println("Usage: ibex [-lawp] [ url | file | directory ]");
+        System.err.println("");
+        System.err.println("    -l <level>      set logging level to { debug, info (default), warn, error, silent }");
+        System.err.println("    -l rpc          log all XML-RPC and SOAP conversations");
+        System.err.println("    -l user@host    email log to user@host");
+        System.err.println("    -l host:port    emit log to TCP socket");
+        System.err.println("    -l <file>       write log to a file on disk");
+        System.err.println("    -a              check assertions");
+        System.err.println("    -w <window-id>  reserved for libibex");
+        System.err.println("    -p              dump profiling information [not yet supported]");
+        Runtime.getRuntime().exit(-1);
+    }
+
+    public static void main(String[] args) throws UnknownHostException, JSExn, IOException {
+        int startargs = 0;
+        while (true) {
+            if (startargs > args.length - 1) printUsage();
+            else if (args[startargs].equals("-a")) JS.checkAssertions = true;
+            else if (args[startargs].equals("-l")) {
+                startargs++;
+                StringTokenizer opts = new StringTokenizer(args[startargs], ",");
+                while(opts.hasMoreTokens()) {
+                    String opt = opts.nextToken();
+                    if (opt.indexOf('@') != -1) Log.email(opt);
+                    else if (opt.indexOf(':') != -1)
+                        Log.tcp(opt.substring(0, opt.indexOf(':')),
+                                Integer.parseInt(opt.substring(opt.indexOf(':') + 1)));
+                    else if (opt.equals("debug")) Log.level = Log.DEBUG;
+                    else if (opt.equals("info")) Log.level = Log.INFO;
+                    else if (opt.equals("warn")) Log.level = Log.WARN;
+                    else if (opt.equals("error")) Log.level = Log.ERROR;
+                    else if (opt.equals("silent")) Log.level = Log.SILENT;
+                    else if (opt.equals("rpc")) Log.rpc = true;
+                    else Log.file(opt);
+                }
+            }
+            else break;
+            startargs++;
+        }
+
+        Platform.forceLoad();
+        if (Log.on) for(int i=0; i<args.length; i++) Log.info(Main.class, "argument " + i + ": " + args[i]);
+
+        initialTemplate = args.length > startargs + 1 ? args[startargs + 1] : "main";
+        origin = args[startargs];
+
+        Stream rr;
+        final String startupTemplate;
+        if (origin.startsWith("http://") || origin.startsWith("https://")) {
+            originHost = origin.substring(origin.indexOf('/') + 2);
+            originHost = originHost.substring(0, originHost.indexOf('/') == -1 ? originHost.length() : originHost.indexOf('/'));
+            if (originHost.indexOf('@') != -1) originHost = originHost.substring(originHost.indexOf('@') + 1);
+            originAddr = InetAddress.getByName(originHost);
+            rr = builtin;
+            startupTemplate = "org.ibex.builtin.splash";
+        } else {
+            rr = new Stream.File(origin);
+            if (!new File(origin).isDirectory()) rr = new Stream.Zip(rr);
+            startupTemplate = initialTemplate;
+        }
+
+        if (Log.on) Log.info(Main.class, "loading xwar");
+        final Ibex ibex = new Ibex(rr);
+
+        scarImage =
+            Picture.load((Stream)Main.builtin.get("org/ibex/builtin/scar.png"),
+                         new Scheduler.Task() { public void perform() throws JSExn, UnknownHostException {
+                             if (Log.on) Log.info(Main.class, "invoking initial template");
+                             ibex.resolveString(startupTemplate, false).call(new Box(), null, null, null, 1);
+                         } });
+
+        Scheduler.init();
+    }
+}