2005-09-19 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / mcs / driver.cs
index c0d6fc48f8ded8fa1a3244b84900d66153d250cf..a8130b6139c977a75001b09a36644b4e5c776f49 100644 (file)
@@ -91,15 +91,11 @@ namespace Mono.CSharp
                static DateTime last_time, first_time;
 
                //
-               // Encoding: ISO-Latin1 is 28591
+               // Encoding.
                //
+               static Encoding defaultEncoding;
                static Encoding encoding;
 
-               //
-               // Whether the user has specified a different encoder manually
-               //
-               static bool using_default_encoder = true;
-
 
                static public void Reset ()
                {
@@ -113,8 +109,7 @@ namespace Mono.CSharp
                        win32ResourceFile = win32IconFile = null;
                        defines = null;
                        output_file = null;
-                       encoding = null;
-                       using_default_encoder = true;
+                       encoding = defaultEncoding = null;
                        first_source = null;
                }
 
@@ -158,7 +153,7 @@ namespace Mono.CSharp
                        }
 
                        using (input){
-                               SeekableStreamReader reader = new SeekableStreamReader (input, encoding, using_default_encoder);
+                               SeekableStreamReader reader = new SeekableStreamReader (input, encoding);
                                Tokenizer lexer = new Tokenizer (reader, file, defines);
                                int token, tokens = 0, errors = 0;
 
@@ -186,8 +181,16 @@ namespace Mono.CSharp
                                return;
                        }
 
-                       SeekableStreamReader reader = new SeekableStreamReader (input, encoding, using_default_encoder);
-                               
+                       SeekableStreamReader reader = new SeekableStreamReader (input, encoding);
+
+                       // Check 'MZ' header
+                       if (reader.Read () == 77 && reader.Read () == 90) {
+                               Report.Error (2015, "Source file `{0}' is a binary file and not a text file", file.Name);
+                               input.Close ();
+                               return;
+                       }
+
+                       reader.Position = 0;
                        parser = new CSharpParser (reader, file, defines);
                        parser.ErrorOutput = Report.Stderr;
                        try {
@@ -276,8 +279,10 @@ namespace Mono.CSharp
 
                public static int counter1, counter2;
                
-       public static int Main (string[] args)
+               public static int Main (string[] args)
                {
+                       Location.InEmacs = Environment.GetEnvironmentVariable ("EMACS") == "t";
+
                        bool ok = MainDriver (args);
                        
                        if (ok && Report.Errors == 0) {
@@ -1296,27 +1301,21 @@ namespace Mono.CSharp
                                return true;
 
                        case "/codepage":
-                               int cp = -1;
-
-                               if (value == "utf8"){
+                               switch (value) {
+                               case "utf8":
                                        encoding = new UTF8Encoding();
-                                       using_default_encoder = false;
-                                       return true;
-                               }
-                               if (value == "reset"){
-                                       //
-                                       // 28591 is the code page for ISO-8859-1 encoding.
-                                       //
-                                       cp = 28591;
-                                       using_default_encoder = true;
-                               }
-                               
-                               try {
-                                       cp = Int32.Parse (value);
-                                       encoding = Encoding.GetEncoding (cp);
-                                       using_default_encoder = false;
-                               } catch {
-                                       Report.Error (2016, "Code page `{0}' is invalid or not installed", value);
+                                       break;
+                               case "reset":
+                                       encoding = defaultEncoding;
+                                       break;
+                               default:
+                                       try {
+                                               encoding = Encoding.GetEncoding (
+                                               Int32.Parse (value));
+                                       } catch {
+                                               Report.Error (2016, "Code page `{0}' is invalid or not installed", value);
+                                       }
+                                       break;
                                }
                                return true;
                        }
@@ -1366,12 +1365,14 @@ namespace Mono.CSharp
                        bool parsing_options = true;
 
                        try {
-                               encoding = Encoding.GetEncoding (28591);
-                       } catch {
-                               Console.WriteLine ("Error: could not load encoding 28591, trying 1252");
-                               encoding = Encoding.GetEncoding (1252);
+                               // Latin1
+                               defaultEncoding = Encoding.GetEncoding (28591);
+                       } catch (Exception) {
+                               // iso-8859-1
+                               defaultEncoding = Encoding.GetEncoding (1252);
                        }
-                       
+                       encoding = defaultEncoding;
+
                        references = new ArrayList ();
                        soft_references = new ArrayList ();
                        modules = new ArrayList ();
@@ -1610,7 +1611,6 @@ namespace Mono.CSharp
                                if (CodeGen.Assembly.IsClsCompliant) {
                                        AttributeTester.VerifyModulesClsCompliance ();
                                        TypeManager.LoadAllImportedTypes ();
-                                       AttributeTester.VerifyTopLevelNameClsCompliance ();
                                }
                        }
                        if (Report.Errors > 0)