fix bug that prevented scar image from loading
[org.ibex.core.git] / src / org / ibex / core / Main.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.core;
6
7 import java.net.*;
8 import java.io.*;
9 import java.util.*;
10 import org.ibex.js.*;
11 import org.ibex.util.*;
12 import org.ibex.plat.*;
13 import org.ibex.graphics.*;
14
15 /** Entry point for the Ibex Engine; handles splash screen, initial xwar loading, and argument processing */
16 public class Main {
17
18     /**
19      *  FEATURE: this should be implemented using self-emulation
20      *  Used for security checks. If this is null, it means that only
21      *  scripts originating from the local filesystem are loaded in
22      *  the engine (maximum permissions). If scripts have been loaded
23      *  from multiple locations, this will be 0.0.0.0 (the invalid
24      *  IP).
25      */
26     public static java.net.InetAddress originAddr = null;
27     public static String originHost = null;
28     public static String origin = null;
29     public static String initialTemplate = null;
30     
31     //public static final Fountain builtin = new Fountain.Zip(new Fountain.FromInputStream(Platform.getBuiltinInputStream()));
32     public static final Fountain vera;
33     static {
34         Fountain temp = null;
35         try {
36             temp = new Fountain.FromInputStream(Encode.JavaSourceCode.decode(Vera.data));
37         } catch (Exception e) { Log.error(Main.class, e); }
38         vera = temp;
39     }
40
41     public static void printUsage() {
42         System.err.println("Usage: ibex [-lawp] [ url | file | directory ]");
43         System.err.println("");
44         System.err.println("    -l <level>      set logging level to { debug, info (default), warn, error, silent }");
45         System.err.println("    -l rpc          log all XML-RPC and SOAP conversations");
46         System.err.println("    -l user@host    email log to user@host");
47         System.err.println("    -l host:port    emit log to TCP socket");
48         System.err.println("    -l <file>       write log to a file on disk");
49         //System.err.println("    -a              check assertions");
50         System.err.println("    -w <window-id>  reserved for libibex");
51         System.err.println("    -p              dump profiling information [not yet supported]");
52         Runtime.getRuntime().exit(-1);
53     }
54
55     public static void main(String[] args) throws UnknownHostException, JSExn, IOException {
56         int startargs = 0;
57         while (true) {
58             if (startargs > args.length - 1) printUsage();
59             // FEATURE: This should be enabled at the parser level - there shouldn't even be an assert bytecode
60             /*else if (args[startargs].equals("-a")) JS.checkAssertions = true;*/
61             else if (args[startargs].equals("-l")) {
62                 startargs++;
63                 StringTokenizer opts = new StringTokenizer(args[startargs], ",");
64                 while(opts.hasMoreTokens()) {
65                     String opt = opts.nextToken();
66                     if (opt.indexOf('@') != -1) Log.email(opt);
67                     else if (opt.indexOf(':') != -1)
68                         Log.tcp(opt.substring(0, opt.indexOf(':')),
69                                 Integer.parseInt(opt.substring(opt.indexOf(':') + 1)));
70                     else if (opt.equals("debug")) Log.level = Log.DEBUG;
71                     else if (opt.equals("info")) Log.level = Log.INFO;
72                     else if (opt.equals("warn")) Log.level = Log.WARN;
73                     else if (opt.equals("error")) Log.level = Log.ERROR;
74                     else if (opt.equals("silent")) Log.level = Log.SILENT;
75                     else if (opt.equals("rpc")) Log.rpc = true;
76                     else Log.file(opt);
77                 }
78             }
79             else break;
80             startargs++;
81         }
82
83         org.ibex.plat.Platform.forceLoad();
84         if (Log.on) for(int i=0; i<args.length; i++) Log.info(Main.class, "argument " + i + ": " + args[i]);
85
86         initialTemplate = args.length > startargs + 1 ? args[startargs + 1] : "main";
87         origin = args[startargs];
88
89         Fountain rr;
90         final String startupTemplate;
91         if (origin.startsWith("http://") || origin.startsWith("https://")) {
92             originHost = origin.substring(origin.indexOf('/') + 2);
93             originHost = originHost.substring(0, originHost.indexOf('/') == -1 ? originHost.length() : originHost.indexOf('/'));
94             if (originHost.indexOf('@') != -1) originHost = originHost.substring(originHost.indexOf('@') + 1);
95             originAddr = InetAddress.getByName(originHost);
96             //rr = builtin;
97             //startupTemplate = "org.ibex.builtin.splash";
98             rr = new Fountain.HTTP(origin);
99             startupTemplate = initialTemplate;
100         } else {
101             rr = new Fountain.File(origin);
102             if (!new File(origin).isDirectory()) rr = new Fountain.Zip(rr);
103             startupTemplate = initialTemplate;
104         }
105
106         if (Log.on) Log.info(Main.class, "loading xwar");
107         final Ibex ibex = new Ibex(rr);
108
109         try {
110             JS blessed = ibex.bless(new Fountain.FromInputStream(Encode.JavaSourceCode.decode(org.ibex.core.builtin.Scar.data)));
111             org.ibex.graphics.Surface.scarImage = Platform.createPicture(blessed);
112         } catch (Exception e) {
113             Log.error(Main.class, e);
114         }
115
116         Platform.Scheduler.add(new Callable() {
117             private final JS[] callargs = new JS[1];
118             public Object run(Object o) throws JSExn,UnknownHostException {
119                 if (Log.on) Log.info(Main.class, "invoking initial template");
120                 try {
121                     callargs[0] = new Box(); 
122                     ibex.resolveString(startupTemplate, false).call(null, callargs);
123                 } finally { callargs[0] = null; }
124                 return null;
125             } });
126     
127         Platform.Scheduler.init();
128     }
129 }