propose-patch
[org.ibex.core.git] / src / org / ibex / Main.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex;
3
4 import java.net.*;
5 import java.io.*;
6 import org.ibex.js.*;
7 import org.ibex.util.*;
8
9 /** Entry point for the Ibex Engine; handles splash screen, initial xwar loading, and argument processing */
10 public class Main {
11
12     /**
13      *  FEATURE: this should be implemented using self-emulation
14      *  Used for security checks. If this is null, it means that only
15      *  scripts originating from the local filesystem are loaded in
16      *  the engine (maximum permissions). If scripts have been loaded
17      *  from multiple locations, this will be 0.0.0.0 (the invalid
18      *  IP).
19      */
20     public static java.net.InetAddress originAddr = null;
21     public static String originHost = null;
22     public static String origin = null;
23     public static String initialTemplate = null;
24     
25     public static final Stream builtin = new Stream.Zip(new Stream.Builtin());
26     public static Picture scarImage = null;
27
28     public static void printUsage() {
29         System.err.println("Usage: ibex [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
30         System.err.println("");
31         System.err.println("Options:");
32         System.err.println("    -v verbose logging (required for logging on Win32)");
33         System.err.println("    -s [not yet supported]");
34         System.err.println("    -l [not yet supported]");
35         System.err.println("");
36         System.err.println("Source-location is one of the following:");
37         System.err.println("    - the path to an xwar file");
38         System.err.println("    - the path to a directory to be used as an xwar");
39         System.err.println("    - the http url of an xwar");
40         System.err.println("");
41         System.err.println("Initial-template is the path of the template to load; defaults to 'main'");
42         Runtime.getRuntime().exit(-1);
43     }
44
45     public static void main(String[] args) throws Exception {
46         int startargs = 0;
47         while (true) {
48             if (startargs > args.length - 1) printUsage();
49             else if (args[startargs].equals("-v")) Log.verbose = true;
50             else break;
51             startargs++;
52         }
53
54         Platform.forceLoad();
55         if (Log.on) for(int i=0; i<args.length; i++) Log.info(Main.class, "argument " + i + ": " + args[i]);
56
57         initialTemplate = args.length > startargs + 1 ? args[startargs + 1] : "main";
58         origin = args[startargs];
59
60         Stream rr;
61         final String startupTemplate;
62         if (origin.startsWith("http://") || origin.startsWith("https://")) {
63             originHost = origin.substring(origin.indexOf('/') + 2);
64             originHost = originHost.substring(0, originHost.indexOf('/') == -1 ? originHost.length() : originHost.indexOf('/'));
65             if (originHost.indexOf('@') != -1) originHost = originHost.substring(originHost.indexOf('@') + 1);
66             originAddr = InetAddress.getByName(originHost);
67             rr = builtin;
68             startupTemplate = "org.ibex.builtin.splash";
69         } else {
70             rr = new Stream.File(origin);
71             if (!new File(origin).isDirectory()) rr = new Stream.Zip(rr);
72             startupTemplate = initialTemplate;
73         }
74
75         if (Log.on) Log.info(Main.class, "loading xwar");
76         final Ibex ibex = new Ibex(rr);
77         final JS final_rr = (JS)ibex.get("");
78
79         scarImage =
80             Picture.load((Stream)Main.builtin.get("org/ibex/builtin/scar.png"),
81                          new Scheduler.Task() { public void perform() throws Exception {
82                              ibex.resolveString(startupTemplate, false).call(new Box(), null, null, null, 1);
83                          } });
84
85         Scheduler.init();
86     }
87 }