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