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