trim down public api
[org.ibex.core.git] / src / org / ibex / core / Main.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.core;
3
4 import java.net.*;
5 import java.io.*;
6 import java.util.*;
7 import org.ibex.js.*;
8 import org.ibex.util.*;
9 import org.ibex.plat.*;
10 import org.ibex.graphics.*;
11
12 /** Entry point for the Ibex Engine; handles splash screen, initial xwar loading, and argument processing */
13 public class Main {
14
15     /**
16      *  FEATURE: this should be implemented using self-emulation
17      *  Used for security checks. If this is null, it means that only
18      *  scripts originating from the local filesystem are loaded in
19      *  the engine (maximum permissions). If scripts have been loaded
20      *  from multiple locations, this will be 0.0.0.0 (the invalid
21      *  IP).
22      */
23     public static java.net.InetAddress originAddr = null;
24     public static String originHost = null;
25     public static String origin = null;
26     public static String initialTemplate = null;
27     
28     public static final Stream builtin = new Stream.Zip(new Stream.Builtin());
29
30     public static void printUsage() {
31         System.err.println("Usage: ibex [-lawp] [ url | file | directory ]");
32         System.err.println("");
33         System.err.println("    -l <level>      set logging level to { debug, info (default), warn, error, silent }");
34         System.err.println("    -l rpc          log all XML-RPC and SOAP conversations");
35         System.err.println("    -l user@host    email log to user@host");
36         System.err.println("    -l host:port    emit log to TCP socket");
37         System.err.println("    -l <file>       write log to a file on disk");
38         //System.err.println("    -a              check assertions");
39         System.err.println("    -w <window-id>  reserved for libibex");
40         System.err.println("    -p              dump profiling information [not yet supported]");
41         Runtime.getRuntime().exit(-1);
42     }
43
44     public static void main(String[] args) throws UnknownHostException, JSExn, IOException {
45         int startargs = 0;
46         while (true) {
47             if (startargs > args.length - 1) printUsage();
48             // FEATURE: This should be enabled at the parser level - there shouldn't even be an assert bytecode
49             /*else if (args[startargs].equals("-a")) JS.checkAssertions = true;*/
50             else if (args[startargs].equals("-l")) {
51                 startargs++;
52                 StringTokenizer opts = new StringTokenizer(args[startargs], ",");
53                 while(opts.hasMoreTokens()) {
54                     String opt = opts.nextToken();
55                     if (opt.indexOf('@') != -1) Log.email(opt);
56                     else if (opt.indexOf(':') != -1)
57                         Log.tcp(opt.substring(0, opt.indexOf(':')),
58                                 Integer.parseInt(opt.substring(opt.indexOf(':') + 1)));
59                     else if (opt.equals("debug")) Log.level = Log.DEBUG;
60                     else if (opt.equals("info")) Log.level = Log.INFO;
61                     else if (opt.equals("warn")) Log.level = Log.WARN;
62                     else if (opt.equals("error")) Log.level = Log.ERROR;
63                     else if (opt.equals("silent")) Log.level = Log.SILENT;
64                     else if (opt.equals("rpc")) Log.rpc = true;
65                     else Log.file(opt);
66                 }
67             }
68             else break;
69             startargs++;
70         }
71
72         org.ibex.plat.Platform.forceLoad();
73         if (Log.on) for(int i=0; i<args.length; i++) Log.info(Main.class, "argument " + i + ": " + args[i]);
74
75         initialTemplate = args.length > startargs + 1 ? args[startargs + 1] : "main";
76         origin = args[startargs];
77
78         Stream rr;
79         final String startupTemplate;
80         if (origin.startsWith("http://") || origin.startsWith("https://")) {
81             originHost = origin.substring(origin.indexOf('/') + 2);
82             originHost = originHost.substring(0, originHost.indexOf('/') == -1 ? originHost.length() : originHost.indexOf('/'));
83             if (originHost.indexOf('@') != -1) originHost = originHost.substring(originHost.indexOf('@') + 1);
84             originAddr = InetAddress.getByName(originHost);
85             rr = builtin;
86             startupTemplate = "org.ibex.builtin.splash";
87         } else {
88             rr = new Stream.File(origin);
89             if (!new File(origin).isDirectory()) rr = new Stream.Zip(rr);
90             startupTemplate = initialTemplate;
91         }
92
93         if (Log.on) Log.info(Main.class, "loading xwar");
94         final Ibex ibex = new Ibex(rr);
95
96         org.ibex.graphics.Surface.scarImage =
97             Picture.load(Main.builtin.get(JS.S("org/ibex/core/builtin/scar.png")),
98                          new Task() { public void perform() throws JSExn, UnknownHostException {
99                              if (Log.on) Log.info(Main.class, "invoking initial template");
100                              ibex.resolveString(startupTemplate, false).call(new Box(), null, null, null, 1);
101                          } });
102
103         Scheduler.init();
104     }
105 }