KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > repl


1 package kawa;
2 import kawa.lang.CompileFile;
3 import java.io.*;
4 import gnu.mapping.*;
5 import gnu.expr.*;
6 import gnu.text.SourceMessages;
7 import gnu.text.SyntaxException;
8 import gnu.lists.*;
9 import java.util.Vector JavaDoc;
10 import gnu.bytecode.ClassType;
11
12 /** Start a "Read-Eval-Print-Loop" for the Kawa Scheme evaluator. */
13
14 public class repl extends Procedure0or1
15 {
16   public static String JavaDoc compilationTopname = null;
17
18   Language language;
19   static Language previousLanguage;
20
21   public repl(Language language)
22   {
23     this.language = language;
24   }
25
26   public Object JavaDoc apply0 ()
27   {
28     Shell.run(language, Environment.getCurrent());
29     return Values.empty;
30   }
31
32   public Object JavaDoc apply1(Object JavaDoc env)
33   {
34     Shell.run(language, (Environment) env);
35     return Values.empty;
36   }
37
38   static void bad_option (String JavaDoc str)
39   {
40     System.err.println ("kawa: bad option '" + str + "'");
41     printOptions(System.err);
42     System.exit (-1);
43   }
44
45   public static void printOption(PrintStream out, String JavaDoc option, String JavaDoc doc)
46   {
47     out.print(" ");
48     out.print(option);
49
50     int len = option.length() + 1;
51     for (int i = 0; i < 30 - len; ++i)
52       out.print(" ");
53     out.print(" ");
54     out.println(doc);
55   }
56   
57   public static void printOptions(PrintStream out)
58   {
59     out.println("Usage: [java kawa.repl | kawa] [options ...]");
60     out.println();
61     out.println(" Generic options:");
62     printOption(out, "--help", "Show help about options");
63     printOption(out, "--author", "Show author information");
64     printOption(out, "--version", "Show version information");
65     out.println();
66     out.println(" Options");
67     printOption(out, "-e <expr>", "Evaluate expression <expr>");
68     printOption(out, "-c <expr>", "Same as -e, but make sure ~/.kawarc.scm is run first");
69     printOption(out, "-f <filename>", "File to interpret");
70     printOption(out, "-s| --", "Start reading commands interactively from console");
71     printOption(out, "-w", "Launch the interpreter in a GUI window");
72     printOption(out, "--server <port>", "Start a server accepting telnet connections on <port>");
73     printOption(out, "--debug-dump-zip", "Compiled interactive expressions to a zip archive");
74     printOption(out, "--debug-print-expr", "Print generated internal expressions");
75     printOption(out, "--debug-print-final-expr", "Print expression after any optimizations");
76     printOption(out, "--debug-error-prints-stack-trace", "Print stack trace with errors");
77     printOption(out, "--debug-warning-prints-stack-trace", "Print stack trace with warnings");
78     printOption(out,"--[no-]full-tailcalls", "(Don't) use full tail-calls");
79     printOption(out, "-C <filename> ...", "Compile named files to Java class files");
80     printOption(out, "--output-format <format>", "Use <format> when printing top-level output");
81     printOption(out,"--<language>", "Select source language, one of:");
82     String JavaDoc[][] languages = Language.getLanguages();
83     for (int i = 0; i < languages.length; i++)
84       {
85     out.print(" ");
86     String JavaDoc[] lang = languages[i];
87     // skip last entry, which is class name
88
int nwords = lang.length - 1;
89     for (int j = 0; j < nwords; j++)
90       out.print(lang[j] + " ");
91     if (i == 0)
92       out.print("[default]");
93     out.println();
94       }
95     out.println(" Compilation options, must be specified before -C");
96     printOption(out, "-d <dirname>", "Directory to place .class files in");
97     printOption(out, "-P <prefix>", "Prefix to prepand to class names");
98     printOption(out, "-T <topname>", "name to give to top-level class");
99     
100     printOption(out, "--main", "Generate an application, with a main method");
101     printOption(out, "--applet", "Generate an applet");
102     printOption(out, "--servlet", "Generate a servlet");
103     printOption(out, "--module-static", "Top-leval definitions are by default static");
104
105     Vector JavaDoc keys = Compilation.options.keys();
106     for (int i = 0; i < keys.size(); ++i)
107       {
108         String JavaDoc name = (String JavaDoc) keys.get(i);
109         printOption(out, "--" + name, Compilation.options.getDoc(name));
110       }
111         
112     out.println();
113     out.println("For more information go to: http://www.gnu.org/software/kawa/");
114   }
115
116   /** Number of times exitDecrement calls before we exit. */
117   private static int exitCounter;
118   /** See exitDecrement. */
119   public static synchronized void exitIncrement()
120   {
121     if (exitCounter == 0)
122       exitCounter++;
123     exitCounter++;
124   }
125
126   /** Work around an AWT bug, where AWT threads are non-daemon.
127    * Thus if you start up AWT, the JVM will wait for the AWT to finish,
128    * even if there are no other non-daemon threads.
129    * So call exitIncrement() each time a Freme is created,
130    * and call exitDecrement() a Frame is closed. */

131   public static synchronized void exitDecrement()
132   {
133     int counter = exitCounter;
134     if (counter > 0)
135       {
136     counter--;
137     if (counter == 0)
138       {
139         System.exit(0);
140       }
141     else
142       exitCounter = counter;
143       }
144   }
145
146   public static String JavaDoc[] commandLineArgArray;
147   public static FVector commandLineArguments;
148
149   public static String JavaDoc homeDirectory;
150
151   static void checkInitFile ()
152   {
153     /* Set homeDirectory; if first time called, run ~/.kawarc.scm. */
154     if (homeDirectory == null)
155       {
156     File initFile = null;
157     homeDirectory = System.getProperty ("user.home");
158     Object JavaDoc scmHomeDirectory;
159     if (homeDirectory != null)
160       {
161         scmHomeDirectory = new FString (homeDirectory);
162         String JavaDoc file_separator = System.getProperty("file.separator");
163         String JavaDoc kawarc_name =
164           "/".equals(file_separator) ? ".kawarc.scm"
165           : "kawarc.scm";
166         initFile = new File(homeDirectory, kawarc_name);
167       }
168     else
169       scmHomeDirectory = Boolean.FALSE;
170     Environment.getCurrent().put("home-directory", scmHomeDirectory);
171     if (initFile != null && initFile.exists())
172       Shell.runFile(initFile.getPath(), 0);
173       }
174   }
175
176   public static void setArgs (String JavaDoc[] args, int arg_start)
177   {
178     int nargs = args.length - arg_start;
179     Object JavaDoc[] array = new Object JavaDoc[nargs];
180     if (arg_start == 0)
181       commandLineArgArray = args;
182     else
183       {
184     String JavaDoc[] strings = new String JavaDoc[nargs];
185     for (int i = nargs; --i >= 0; )
186       strings[i] = args[i+arg_start];
187     commandLineArgArray = strings;
188       }
189     for (int i = nargs; --i >= 0; )
190       array[i] = new FString (args[i + arg_start]);
191     commandLineArguments = new FVector (array); // FIXME scsh has list
192
// FIXME scsh also has command-line proc
193
Environment.getCurrent().put("command-line-arguments",
194                  commandLineArguments);
195   }
196
197   public static void getLanguageFromFilenameExtension(String JavaDoc name)
198   {
199     if (previousLanguage == null)
200       {
201     previousLanguage = Language.getInstanceFromFilenameExtension(name);
202     if (previousLanguage != null)
203       {
204         Language.setDefaults(previousLanguage);
205         return;
206       }
207       }
208     getLanguage();
209   }
210
211   public static void getLanguage()
212   {
213     if (previousLanguage == null)
214       {
215     previousLanguage = Language.getInstance(null);
216     Language.setDefaults(previousLanguage);
217       }
218   }
219
220   static boolean shutdownRegistered
221     = gnu.text.WriterManager.instance.registerShutdownHook();
222
223   public static int processArgs(String JavaDoc[] args, int iArg, int maxArg)
224   {
225     boolean something_done = false;
226     for ( ; iArg < maxArg; iArg++)
227       {
228     String JavaDoc arg = args[iArg];
229     if (arg.equals ("-c") || arg.equals ("-e"))
230       {
231         iArg++;
232         if (iArg == maxArg)
233           bad_option (arg);
234         getLanguage();
235         setArgs (args, iArg+1);
236         if (arg.equals ("-c"))
237           checkInitFile();
238         Language language = Language.getDefaultLanguage();
239         Shell.runString(args[iArg], language, Environment.getCurrent());
240         something_done = true;
241       }
242     else if (arg.equals ("-f"))
243       {
244         iArg++;
245         if (iArg == maxArg)
246           bad_option (arg);
247         String JavaDoc filename = args[iArg];
248         getLanguageFromFilenameExtension(filename);
249         setArgs (args, iArg+1);
250         checkInitFile();
251         Shell.runFile (filename, 0);
252         something_done = true;
253       }
254     else if (arg.startsWith("--script"))
255       {
256             String JavaDoc count = arg.substring(8);
257         iArg++;
258             int skipLines = 0;
259             if (count.length() > 0)
260               {
261                 try
262                   {
263                     skipLines = Integer.parseInt(count);
264                   }
265                 catch (Throwable JavaDoc ex)
266                   {
267                     iArg = maxArg; // force bad_option.
268
}
269               }
270         if (iArg == maxArg)
271           bad_option (arg);
272         String JavaDoc filename = args[iArg];
273         getLanguageFromFilenameExtension(filename);
274         setArgs (args, iArg+1);
275         checkInitFile();
276         Shell.runFile(filename, skipLines);
277             return -1;
278       }
279     else if (arg.equals("\\"))
280       {
281         // Scsh-like "meta-arg". See Kawa manual (SOON-FIXME).
282
if (++iArg == maxArg)
283           bad_option (arg);
284         String JavaDoc filename = args[iArg];
285         InPort freader;
286         try
287           {
288         InputStream fstream = new BufferedInputStream(new FileInputStream(filename));
289         int ch = fstream.read();
290         if (ch == '#')
291           {
292             StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(100);
293             Vector JavaDoc xargs = new Vector JavaDoc(10);
294             int state = 0;
295             while (ch != '\n' && ch != '\r' && ch >= 0)
296               ch = fstream.read();
297             for (;;)
298               {
299             ch = fstream.read();
300             if (ch < 0)
301               {
302                 System.err.println("unexpected end-of-file processing argument line for: '" + filename + '\'');
303                 System.exit(-1);
304               }
305             if (state == 0)
306               {
307                 if (ch == '\\' || ch == '\'' || ch == '\"')
308                   {
309                 state = ch;
310                 continue;
311                   }
312                 else if (ch == '\n' || ch == '\r')
313                   break;
314                 else if (ch == ' ' || ch == '\t')
315                   {
316                 if (sbuf.length() > 0)
317                   {
318                     xargs.addElement(sbuf.toString());
319                     sbuf.setLength(0);
320                   }
321                 continue;
322                   }
323               }
324             else if (state == '\\')
325               state = 0;
326             else if (ch == state)
327               {
328                 state = 0;
329                 continue;
330               }
331             sbuf.append((char) ch);
332               }
333             if (sbuf.length() > 0)
334               xargs.addElement(sbuf.toString());
335             int nxargs = xargs.size();
336             if (nxargs > 0)
337               {
338             String JavaDoc[] sargs = new String JavaDoc[nxargs];
339             xargs.copyInto(sargs);
340             int ixarg = processArgs(sargs, 0, nxargs);
341             if (ixarg >= 0 && ixarg < nxargs)
342               { // FIXME
343
System.err.println(""+(nxargs-ixarg)+" unused meta args");
344               }
345               }
346           }
347         getLanguageFromFilenameExtension(filename);
348         freader = InPort.openFile(fstream, filename);
349         // FIXME adjust line number
350
setArgs(args, iArg+1);
351         checkInitFile();
352         kawa.standard.load.loadSource(freader, Environment.user(), null);
353         return -1;
354           }
355         catch (SyntaxException ex)
356           {
357         ex.printAll(OutPort.errDefault(), 20);
358           }
359         catch (java.io.FileNotFoundException JavaDoc ex)
360           {
361         System.err.println("Cannot open file "+filename);
362         System.exit(1);
363           }
364         catch (Throwable JavaDoc ex)
365           {
366         ex.printStackTrace(System.err);
367         System.exit(1);
368           }
369         return -1;
370       }
371     else if (arg.equals ("-s") || arg.equals ("--"))
372       {
373         iArg++;
374         getLanguage();
375         setArgs (args, iArg);
376         checkInitFile();
377         Shell.run(Language.getDefaultLanguage(), Environment.getCurrent());
378         return -1;
379       }
380     else if (arg.equals ("-w"))
381       {
382         iArg++;
383         getLanguage();
384         setArgs (args, iArg);
385         checkInitFile();
386         // Do this instead of just new GuiConsole in case we have
387
// configured --without-awt.
388
try
389           {
390         Class.forName("kawa.GuiConsole").newInstance();
391           }
392         catch (Exception JavaDoc ex)
393           {
394         System.err.println("failed to create Kawa window: "+ex);
395         System.exit (-1);
396           }
397         something_done = true;
398       }
399     else if (arg.equals ("-d"))
400       {
401         iArg++;
402         if (iArg == maxArg)
403           bad_option (arg);
404             ModuleManager manager = ModuleManager.getInstance();
405         manager.setCompilationDirectory(args[iArg]);
406       }
407     else if (arg.equals ("-P"))
408       {
409         iArg++;
410         if (iArg == maxArg)
411           bad_option (arg);
412         Compilation.classPrefixDefault = args[iArg];
413       }
414     else if (arg.equals ("-T"))
415       {
416         iArg++;
417         if (iArg == maxArg)
418           bad_option (arg);
419         compilationTopname = args[iArg];
420       }
421     else if (arg.equals ("-C"))
422       {
423         ++iArg;
424         if (iArg == maxArg)
425           bad_option (arg);
426             compileFiles(args, iArg, maxArg);
427         return -1;
428       }
429     else if (arg.equals("--output-format")
430          || arg.equals("--format"))
431       {
432         if (++iArg == maxArg)
433           bad_option (arg);
434         Shell.setDefaultFormat(args[iArg]);
435       }
436     else if (arg.equals("--connect"))
437       {
438         ++iArg;
439         if (iArg == maxArg)
440           bad_option (arg);
441         int port;
442         if (args[iArg].equals("-"))
443           port = 0;
444         else
445           {
446         try
447           {
448             port = Integer.parseInt(args[iArg]);
449           }
450         catch (NumberFormatException JavaDoc ex)
451           {
452             bad_option ("--connect port#");
453             port = -1; // never seen.
454
}
455           }
456         try
457           {
458         java.net.Socket JavaDoc socket = new java.net.Socket JavaDoc("localhost",port);
459         Telnet conn = new Telnet(socket, true);
460         java.io.InputStream JavaDoc sin = conn.getInputStream();
461         java.io.OutputStream JavaDoc sout = conn.getOutputStream();
462         java.io.PrintStream JavaDoc pout = new PrintStream (sout, true);
463         System.setIn(sin);
464         System.setOut(pout);
465         System.setErr(pout);
466           }
467         catch (java.io.IOException JavaDoc ex)
468           {
469         ex.printStackTrace(System.err);
470         throw new Error JavaDoc(ex.toString());
471           }
472       }
473     else if (arg.equals("--server"))
474       {
475         getLanguage();
476         ++iArg;
477         if (iArg == maxArg)
478           bad_option (arg);
479         int port;
480         if (args[iArg].equals("-"))
481           port = 0;
482         else
483           {
484         try
485           {
486             port = Integer.parseInt(args[iArg]);
487           }
488         catch (NumberFormatException JavaDoc ex)
489           {
490             bad_option ("--server port#");
491             port = -1; // never seen.
492
}
493           }
494         try
495           {
496         java.net.ServerSocket JavaDoc ssocket
497           = new java.net.ServerSocket JavaDoc(port);
498         port = ssocket.getLocalPort();
499         System.err.println("Listening on port "+port);
500         for (;;)
501           {
502             System.err.print("waiting ... "); System.err.flush();
503             java.net.Socket JavaDoc client = ssocket.accept();
504             System.err.println("got connection from "
505                        +client.getInetAddress()
506                        +" port:"+client.getPort());
507             TelnetRepl.serve(Language.getDefaultLanguage(), client);
508           }
509           }
510         catch (java.io.IOException JavaDoc ex)
511           {
512         throw new Error JavaDoc(ex.toString());
513           }
514       }
515     else if (arg.equals("--main"))
516       {
517         Compilation.generateMainDefault = true;
518       }
519     else if (arg.equals("--applet"))
520       {
521         Compilation.generateAppletDefault = true;
522       }
523     else if (arg.equals("--servlet"))
524       {
525         Compilation.generateServletDefault = true;
526       }
527     else if (arg.equals("--debug-dump-zip"))
528       {
529         gnu.expr.ModuleExp.dumpZipPrefix = "kawa-zip-dump-";
530       }
531     else if (arg.equals("--debug-print-expr"))
532       {
533             Compilation.debugPrintExpr = true;
534       }
535     else if (arg.equals("--debug-print-final-expr"))
536       {
537         Compilation.debugPrintFinalExpr = true;
538       }
539     else if (arg.equals("--debug-error-prints-stack-trace"))
540       {
541             SourceMessages.debugStackTraceOnError = true;
542       }
543     else if (arg.equals("--debug-warning-prints-stack-trace"))
544       {
545             SourceMessages.debugStackTraceOnWarning = true;
546       }
547     else if (arg.equals("--module-static"))
548       {
549         gnu.expr.Compilation.moduleStatic = 1;
550       }
551         else if (arg.equals("--module-static-run"))
552           {
553             gnu.expr.Compilation.moduleStatic = 2;
554           }
555     else if (arg.equals("--fewer-classes"))
556       {
557         gnu.expr.Compilation.fewerClasses = true;
558       }
559     else if (arg.equals("--no-inline")
560          || arg.equals("--inline=none"))
561       {
562         gnu.expr.Compilation.inlineOk = false;
563       }
564     else if (arg.equals("--inline"))
565       {
566         gnu.expr.Compilation.inlineOk = true;
567       }
568     else if (arg.equals("--cps"))
569       {
570         Compilation.fewerClasses = true;
571         Compilation.defaultCallConvention
572           = Compilation.CALL_WITH_CONTINUATIONS;
573       }
574     else if (arg.equals("--full-tailcalls"))
575       {
576         Compilation.defaultCallConvention
577           = Compilation.CALL_WITH_TAILCALLS;
578       }
579     else if (arg.equals("--no-full-tailcalls"))
580       {
581         Compilation.defaultCallConvention
582           = Compilation.CALL_WITH_RETURN;
583       }
584         else if (arg.equals("--pedantic"))
585           {
586             Language.requirePedantic = true;
587           }
588     else if (arg.equals("--help"))
589       {
590         printOptions(System.out);
591         System.exit(0);
592       }
593     else if (arg.equals("--author"))
594       {
595         System.out.println("Per Bothner <per@bothner.com>");
596         System.exit(0);
597       }
598     else if (arg.equals("--version"))
599       {
600         System.out.print("Kawa ");
601         System.out.print(Version.getVersion());
602         System.out.println();
603         System.out.println("Copyright (C) 2007 Per Bothner");
604         something_done = true;
605       }
606     else if (arg.length () > 0 && arg.charAt(0) == '-')
607       { // Check if arg is a known language name.
608
String JavaDoc name = arg;
609         if (name.length() > 2 && name.charAt(0) == '-')
610           name = name.substring(name.charAt(1) == '-' ? 2 :1);
611         Language lang = Language.getInstance(name);
612         if (lang != null)
613           {
614         if (previousLanguage == null)
615           Language.setDefaults(lang);
616                 else
617                   Language.setDefaultLanguage(lang);
618         previousLanguage = lang;
619           }
620         else
621           {
622         // See if arg is a valid Compilation option, and if so set it.
623
int eq = name.indexOf("=");
624         String JavaDoc opt_value;
625         if (eq < 0)
626           opt_value = null;
627         else
628           {
629             opt_value = name.substring(eq+1);
630             name = name.substring(0, eq);
631           }
632
633         // Convert "--no-xxx" to "--xxx=no":
634
boolean startsWithNo
635           = name.startsWith("no-") && name.length() > 3;
636         if (opt_value == null && startsWithNo)
637           {
638             opt_value = "no";
639             name = name.substring(3);
640           }
641
642         String JavaDoc msg = Compilation.options.set(name, opt_value);
643         if (msg != null)
644           {
645             // It wasn't a valid Complation option.
646
if (startsWithNo && msg == gnu.text.Options.UNKNOWN)
647               msg = "both '--no-' prefix and '="+
648             opt_value+"' specified";
649             if (msg == gnu.text.Options.UNKNOWN)
650               {
651             bad_option(arg);
652               }
653             else
654               {
655             System.err.println ("kawa: bad option '"
656                         + arg + "': " + msg);
657             System.exit (-1);
658               }
659           }
660           }
661       }
662     else
663           {
664             int ci = arg.indexOf('=');
665             if (ci <= 0)
666               return iArg;
667             String JavaDoc key = arg.substring(0, ci);
668             String JavaDoc value = arg.substring(ci+1);
669             String JavaDoc uri, local, prefix = "";
670             for (int i = 0; ; i++)
671               {
672                 String JavaDoc[] propertyField = propertyFields[i];
673                 if (propertyField == null)
674                   break;
675                 if (key.equals(propertyField[0]))
676                   {
677                     String JavaDoc cname = propertyField[1];
678                     String JavaDoc fname = propertyField[2];
679                     try
680                       {
681                         Class JavaDoc clas = Class.forName(cname);
682                         ThreadLocation loc = (ThreadLocation)
683                           clas.getDeclaredField(fname).get(null);
684                         loc.setGlobal(value);
685                         break;
686                       }
687                     catch (Throwable JavaDoc ex)
688                       {
689                         System.err.println("error setting property " + key
690                                            +" field "+cname+'.'+fname+": "+ex);
691                         System.exit(-1);
692                       }
693                   }
694               }
695             Symbol symbol = Symbol.parse(key);
696             // Run Language's static initializer.
697
Language.getDefaultLanguage();
698             Environment current = Environment.getCurrent();
699             current.put(symbol, null, value);
700           }
701       }
702     return something_done ? -1 : iArg;
703   }
704
705   public static void compileFiles (String JavaDoc[] args, int iArg, int maxArg)
706   {
707     ModuleManager manager = ModuleManager.getInstance();
708     Compilation[] comps = new Compilation[maxArg-iArg];
709     ModuleInfo[] infos = new ModuleInfo[maxArg-iArg];
710     SourceMessages messages = new SourceMessages();
711     for (int i = iArg; i < maxArg; i++)
712       {
713         String JavaDoc arg = args[i];
714         getLanguageFromFilenameExtension(arg);
715         Language language = Language.getDefaultLanguage();
716         try
717           {
718             InPort fstream;
719             try
720               {
721                 fstream = InPort.openFile(arg);
722               }
723             catch (java.io.FileNotFoundException JavaDoc ex)
724               {
725                 System.err.println(ex);
726                 System.exit(-1);
727                 break; // Kludge to shut up compiler.
728
}
729             
730             Compilation comp
731               = language.parse(fstream, messages, Language.PARSE_PROLOG);
732
733             if (compilationTopname != null)
734               {
735                 String JavaDoc cname
736                   = Compilation.mangleNameIfNeeded(compilationTopname);
737                 ClassType ctype = new ClassType(cname);
738                 ModuleExp mexp = comp.getModule();
739                 mexp.setType(ctype);
740                 mexp.setName(compilationTopname);
741                 comp.mainClass = ctype;
742               }
743             
744             infos[i-iArg] = manager.find(comp);
745             comps[i-iArg] = comp;
746
747           }
748         catch (Throwable JavaDoc ex)
749           {
750             if (! (ex instanceof SyntaxException)
751                 || ((SyntaxException) ex).getMessages() != messages)
752               {
753                 System.err.println("Internal error while compiling "+arg);
754                 ex.printStackTrace(System.err);
755                 System.exit(-1);
756               }
757           }
758         if (messages.seenErrorsOrWarnings())
759           {
760             System.err.println("(compiling "+arg+')');
761             if (messages.checkErrors(System.err, 20))
762               System.exit(1);
763           }
764       }
765
766     for (int i = iArg; i < maxArg; i++)
767       {
768         String JavaDoc arg = args[i];
769         Compilation comp = comps[i-iArg];
770         try
771           {
772             System.err.println("(compiling "+arg+" to "+comp.mainClass.getName()+')');
773
774             infos[i-iArg].loadByStages(Compilation.CLASS_WRITTEN);
775             boolean sawErrors = messages.seenErrors();
776             messages.checkErrors(System.err, 50);
777             if (sawErrors)
778               System.exit(-1);
779             comps[i-iArg] = comp;
780             sawErrors = messages.seenErrors();
781             messages.checkErrors(System.err, 50);
782             if (sawErrors)
783               System.exit(-1);
784           }
785         catch (Throwable JavaDoc ex)
786           {
787             StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
788             String JavaDoc file = comp.getFileName();
789             int line = comp.getLineNumber();
790             if (file != null && line > 0)
791               {
792                 sbuf.append(file);
793                 sbuf.append(':');
794                 sbuf.append(line);
795                 sbuf.append(": ");
796               }
797             sbuf.append("internal error while compiling ");
798             sbuf.append(arg);
799             System.err.println(sbuf.toString());
800             ex.printStackTrace(System.err);
801             System.exit(-1);
802           }
803       }
804   }
805
806   /** A list of standard command-line fluid names to map to static fields.
807    * For each entry:
808    * element 0 is a property name (before the '=' in the comamnd-line);
809    * element 1 is the name of a class;
810    * element 2 is the name of a static ThreadLocation field. */

811   static String JavaDoc[][] propertyFields =
812     {
813       { "out:doctype-system", "gnu.xml.XMLPrinter", "doctypeSystem" },
814       { "out:doctype-public", "gnu.xml.XMLPrinter", "doctypePublic" },
815       { "out:base", "gnu.kawa.functions.DisplayFormat", "outBase" },
816       { "out:radix", "gnu.kawa.functions.DisplayFormat", "outRadix" },
817       { "out:line-length", "gnu.text.PrettyWriter", "lineLengthLoc" },
818       { "out:right-margin", "gnu.text.PrettyWriter", "lineLengthLoc" },
819       { "out:miser-width", "gnu.text.PrettyWriter", "miserWidthLoc" },
820       { "out:xml-indent", "gnu.xml.XMLPrinter", "indentLoc" },
821       { "display:toolkit", "gnu.kawa.models.Display", "myDisplay" },
822       null
823     };
824
825   public static void main(String JavaDoc args[])
826   {
827     try
828       {
829     int iArg = processArgs(args, 0, args.length);
830     if (iArg < 0)
831       return;
832     if (iArg < args.length)
833       {
834         String JavaDoc filename = args[iArg];
835         getLanguageFromFilenameExtension(filename);
836         setArgs (args, iArg+1);
837         checkInitFile();
838         Shell.runFile(filename, 0);
839       }
840     else
841       {
842         getLanguage();
843         setArgs (args, iArg);
844         checkInitFile();
845         Shell.run(Language.getDefaultLanguage(), Environment.getCurrent());
846       }
847       }
848     finally
849       {
850     if (! shutdownRegistered)
851       {
852         // Redundant if registerShutdownHook succeeded (e.g on JDK 1.3).
853
gnu.mapping.OutPort.runCleanups();
854       }
855     exitDecrement();
856       }
857    }
858 }
859
Popular Tags