Merge pull request #869 from alistair/copyfile
[mono.git] / mcs / tools / csharp / repl.cs
1 //
2 // repl.cs: Support for using the compiler in interactive mode (read-eval-print loop)
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@gnome.org)
6 //
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2004, 2005, 2006, 2007, 2008 Novell, Inc
11 // Copyright 2011-2013 Xamarin Inc
12 //
13 //
14 // TODO:
15 //   Do not print results in Evaluate, do that elsewhere in preparation for Eval refactoring.
16 //   Driver.PartialReset should not reset the coretypes, nor the optional types, to avoid
17 //      computing that on every call.
18 //
19 using System;
20 using System.IO;
21 using System.Text;
22 using System.Globalization;
23 using System.Collections;
24 using System.Reflection;
25 using System.Reflection.Emit;
26 using System.Threading;
27 using System.Net;
28 using System.Net.Sockets;
29 using System.Collections.Generic;
30
31 using Mono.CSharp;
32
33 namespace Mono {
34
35         public class Driver {
36                 public static string StartupEvalExpression;
37                 static int? attach;
38                 static string target_host;
39                 static int target_port;
40                 static string agent;
41                 
42                 static int Main (string [] args)
43                 {
44                         var cmd = new CommandLineParser (Console.Out);
45                         cmd.UnknownOptionHandler += HandleExtraArguments;
46
47                         // Enable unsafe code by default
48                         var settings = new CompilerSettings () {
49                                 Unsafe = true
50                         };
51
52                         if (!cmd.ParseArguments (settings, args))
53                                 return 1;
54
55                         var startup_files = new string [settings.SourceFiles.Count];
56                         int i = 0;
57                         foreach (var source in settings.SourceFiles)
58                                 startup_files [i++] = source.FullPathName;
59                         settings.SourceFiles.Clear ();
60
61                         TextWriter agent_stderr = null;
62                         ReportPrinter printer;
63                         if (agent != null) {
64                                 agent_stderr = new StringWriter ();
65                                 printer = new StreamReportPrinter (agent_stderr);
66                         } else {
67                                 printer = new ConsoleReportPrinter ();
68                         }
69
70                         var eval = new Evaluator (new CompilerContext (settings, printer));
71
72                         eval.InteractiveBaseClass = typeof (InteractiveBaseShell);
73                         eval.DescribeTypeExpressions = true;
74                         eval.WaitOnTask = true;
75
76                         CSharpShell shell;
77 #if !ON_DOTNET
78                         if (attach.HasValue) {
79                                 shell = new ClientCSharpShell_v1 (eval, attach.Value);
80                         } else if (agent != null) {
81                                 new CSharpAgent (eval, agent, agent_stderr).Run (startup_files);
82                                 return 0;
83                         } else
84 #endif
85                         if (target_host != null) 
86                                 shell = new ClientCSharpShell  (eval, target_host, target_port);
87                         else 
88                                 shell = new CSharpShell (eval);
89
90                         return shell.Run (startup_files);
91                 }
92
93                 static int HandleExtraArguments (string [] args, int pos)
94                 {
95                         switch (args [pos]) {
96                         case "-e":
97                                 if (pos + 1 < args.Length) {
98                                         StartupEvalExpression = args[pos + 1];
99                                         return pos + 1;
100                                 }
101                                 break;
102                         case "--attach":
103                                 if (pos + 1 < args.Length) {
104                                         attach = Int32.Parse (args[1]);
105                                         return pos + 1;
106                                 }
107                                 break;
108                         default:
109                                 if (args [pos].StartsWith ("--server=")){
110                                         var hostport = args [pos].Substring (9);
111                                         int p = hostport.IndexOf (':');
112                                         if (p == -1){
113                                                 target_host = hostport;
114                                                 target_port = 10000;
115                                         } else {
116                                                 target_host = hostport.Substring (0,p);
117                                                 if (!int.TryParse (hostport.Substring (p), out target_port)){
118                                                         Console.Error.WriteLine ("Usage is: --server[=host[:port]");
119                                                         Environment.Exit (1);
120                                                 }
121                                         }
122                                         return pos + 1;
123                                 }
124                                 if (args [pos].StartsWith ("--client")){
125                                         target_host = "localhost";
126                                         target_port = 10000;
127                                         return pos + 1;
128                                 }
129                                 if (args [pos].StartsWith ("--agent:")) {
130                                         agent = args[pos];
131                                         return pos + 1;
132                                 } else {
133                                         return -1;
134                                 }
135                         }
136                         return -1;
137                 }
138                 
139         }
140
141         public class InteractiveBaseShell : InteractiveBase {
142                 static bool tab_at_start_completes;
143                 
144                 static InteractiveBaseShell ()
145                 {
146                         tab_at_start_completes = false;
147                 }
148
149                 internal static Mono.Terminal.LineEditor Editor;
150                 
151                 public static bool TabAtStartCompletes {
152                         get {
153                                 return tab_at_start_completes;
154                         }
155
156                         set {
157                                 tab_at_start_completes = value;
158                                 if (Editor != null)
159                                         Editor.TabAtStartCompletes = value;
160                         }
161                 }
162
163                 public static new string help {
164                         get {
165                                 return InteractiveBase.help +
166                                         "  TabAtStartCompletes      - Whether tab will complete even on empty lines\n";
167                         }
168                 }
169         }
170         
171         public class CSharpShell {
172                 static bool isatty = true, is_unix = false;
173                 protected string [] startup_files;
174                 
175                 Mono.Terminal.LineEditor editor;
176                 bool dumb;
177                 readonly Evaluator evaluator;
178
179                 public CSharpShell (Evaluator evaluator)
180                 {
181                         this.evaluator = evaluator;
182                 }
183
184                 protected virtual void ConsoleInterrupt (object sender, ConsoleCancelEventArgs a)
185                 {
186                         // Do not about our program
187                         a.Cancel = true;
188
189                         evaluator.Interrupt ();
190                 }
191                 
192                 void SetupConsole ()
193                 {
194                         if (is_unix){
195                                 string term = Environment.GetEnvironmentVariable ("TERM");
196                                 dumb = term == "dumb" || term == null || isatty == false;
197                         } else
198                                 dumb = false;
199                         
200                         editor = new Mono.Terminal.LineEditor ("csharp", 300);
201                         InteractiveBaseShell.Editor = editor;
202
203                         editor.AutoCompleteEvent += delegate (string s, int pos){
204                                 string prefix = null;
205
206                                 string complete = s.Substring (0, pos);
207                                 
208                                 string [] completions = evaluator.GetCompletions (complete, out prefix);
209                                 
210                                 return new Mono.Terminal.LineEditor.Completion (prefix, completions);
211                         };
212                         
213 #if false
214                         //
215                         // This is a sample of how completions sould be implemented.
216                         //
217                         editor.AutoCompleteEvent += delegate (string s, int pos){
218
219                                 // Single match: "Substring": Sub-string
220                                 if (s.EndsWith ("Sub")){
221                                         return new string [] { "string" };
222                                 }
223
224                                 // Multiple matches: "ToString" and "ToLower"
225                                 if (s.EndsWith ("T")){
226                                         return new string [] { "ToString", "ToLower" };
227                                 }
228                                 return null;
229                         };
230 #endif
231                         
232                         Console.CancelKeyPress += ConsoleInterrupt;
233                 }
234
235                 string GetLine (bool primary)
236                 {
237                         string prompt = primary ? InteractiveBase.Prompt : InteractiveBase.ContinuationPrompt;
238
239                         if (dumb){
240                                 if (isatty)
241                                         Console.Write (prompt);
242
243                                 return Console.ReadLine ();
244                         } else {
245                                 return editor.Edit (prompt, "");
246                         }
247                 }
248
249                 delegate string ReadLiner (bool primary);
250
251                 void InitializeUsing ()
252                 {
253                         Evaluate ("using System; using System.Linq; using System.Collections.Generic; using System.Collections;");
254                 }
255
256                 void InitTerminal (bool show_banner)
257                 {
258                         int p = (int) Environment.OSVersion.Platform;
259                         is_unix = (p == 4) || (p == 128);
260
261 #if NET_4_5
262                         isatty = !Console.IsInputRedirected && !Console.IsOutputRedirected;
263 #else
264                         isatty = true;
265 #endif
266
267                         // Work around, since Console is not accounting for
268                         // cursor position when writing to Stderr.  It also
269                         // has the undesirable side effect of making
270                         // errors plain, with no coloring.
271 //                      Report.Stderr = Console.Out;
272                         SetupConsole ();
273
274                         if (isatty && show_banner)
275                                 Console.WriteLine ("Mono C# Shell, type \"help;\" for help\n\nEnter statements below.");
276
277                 }
278
279                 void ExecuteSources (IEnumerable<string> sources, bool ignore_errors)
280                 {
281                         foreach (string file in sources){
282                                 try {
283                                         try {
284                                                 bool first = true;
285                         
286                                                 using (System.IO.StreamReader r = System.IO.File.OpenText (file)){
287                                                         ReadEvalPrintLoopWith (p => {
288                                                                 var line = r.ReadLine ();
289                                                                 if (first){
290                                                                         if (line.StartsWith ("#!"))
291                                                                                 line = r.ReadLine ();
292                                                                         first = false;
293                                                                 }
294                                                                 return line;
295                                                         });
296                                                 }
297                                         } catch (FileNotFoundException){
298                                                 Console.Error.WriteLine ("cs2001: Source file `{0}' not found", file);
299                                                 return;
300                                         }
301                                 } catch {
302                                         if (!ignore_errors)
303                                                 throw;
304                                 }
305                         }
306                 }
307                 
308                 protected virtual void LoadStartupFiles ()
309                 {
310                         string dir = Path.Combine (
311                                 Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData),
312                                 "csharp");
313                         if (!Directory.Exists (dir))
314                                 return;
315
316                         List<string> sources = new List<string> ();
317                         List<string> libraries = new List<string> ();
318                         
319                         foreach (string file in System.IO.Directory.GetFiles (dir)){
320                                 string l = file.ToLower ();
321                                 
322                                 if (l.EndsWith (".cs"))
323                                         sources.Add (file);
324                                 else if (l.EndsWith (".dll"))
325                                         libraries.Add (file);
326                         }
327
328                         foreach (string file in libraries)
329                                 evaluator.LoadAssembly (file);
330
331                         ExecuteSources (sources, true);
332                 }
333
334                 void ReadEvalPrintLoopWith (ReadLiner readline)
335                 {
336                         string expr = null;
337                         while (!InteractiveBase.QuitRequested){
338                                 string input = readline (expr == null);
339                                 if (input == null)
340                                         return;
341
342                                 if (input == "")
343                                         continue;
344
345                                 expr = expr == null ? input : expr + "\n" + input;
346                                 
347                                 expr = Evaluate (expr);
348                         }
349                 }
350
351                 public int ReadEvalPrintLoop ()
352                 {
353                         if (startup_files != null && startup_files.Length == 0)
354                                 InitTerminal (startup_files.Length == 0 && Driver.StartupEvalExpression == null);
355
356                         InitializeUsing ();
357
358                         LoadStartupFiles ();
359
360                         if (startup_files != null && startup_files.Length != 0) {
361                                 ExecuteSources (startup_files, false);
362                         } else {
363                                 if (Driver.StartupEvalExpression != null){
364                                         ReadEvalPrintLoopWith (p => {
365                                                 var ret = Driver.StartupEvalExpression;
366                                                 Driver.StartupEvalExpression = null;
367                                                 return ret;
368                                                 });
369                                 } else {
370                                         ReadEvalPrintLoopWith (GetLine);
371                                 }
372                                 
373                                 editor.SaveHistory ();
374                         }
375
376                         Console.CancelKeyPress -= ConsoleInterrupt;
377                         
378                         return 0;
379                 }
380
381                 protected virtual string Evaluate (string input)
382                 {
383                         bool result_set;
384                         object result;
385
386                         try {
387                                 input = evaluator.Evaluate (input, out result, out result_set);
388
389                                 if (result_set){
390                                         PrettyPrint (Console.Out, result);
391                                         Console.WriteLine ();
392                                 }
393                         } catch (Exception e){
394                                 Console.WriteLine (e);
395                                 return null;
396                         }
397                         
398                         return input;
399                 }
400
401                 static void p (TextWriter output, string s)
402                 {
403                         output.Write (s);
404                 }
405
406                 static string EscapeString (string s)
407                 {
408                         return s.Replace ("\"", "\\\"");
409                 }
410                 
411                 static void EscapeChar (TextWriter output, char c)
412                 {
413                         if (c == '\''){
414                                 output.Write ("'\\''");
415                                 return;
416                         }
417                         if (c > 32){
418                                 output.Write ("'{0}'", c);
419                                 return;
420                         }
421                         switch (c){
422                         case '\a':
423                                 output.Write ("'\\a'");
424                                 break;
425
426                         case '\b':
427                                 output.Write ("'\\b'");
428                                 break;
429                                 
430                         case '\n':
431                                 output.Write ("'\\n'");
432                                 break;
433                                 
434                         case '\v':
435                                 output.Write ("'\\v'");
436                                 break;
437                                 
438                         case '\r':
439                                 output.Write ("'\\r'");
440                                 break;
441                                 
442                         case '\f':
443                                 output.Write ("'\\f'");
444                                 break;
445                                 
446                         case '\t':
447                                 output.Write ("'\\t");
448                                 break;
449
450                         default:
451                                 output.Write ("'\\x{0:x}", (int) c);
452                                 break;
453                         }
454                 }
455
456                 // Some types (System.Json.JsonPrimitive) implement
457                 // IEnumerator and yet, throw an exception when we
458                 // try to use them, helper function to check for that
459                 // condition
460                 static internal bool WorksAsEnumerable (object obj)
461                 {
462                         IEnumerable enumerable = obj as IEnumerable;
463                         if (enumerable != null){
464                                 try {
465                                         enumerable.GetEnumerator ();
466                                         return true;
467                                 } catch {
468                                         // nothing, we return false below
469                                 }
470                         }
471                         return false;
472                 }
473                 
474                 internal static void PrettyPrint (TextWriter output, object result)
475                 {
476                         if (result == null){
477                                 p (output, "null");
478                                 return;
479                         }
480                         
481                         if (result is Array){
482                                 Array a = (Array) result;
483                                 
484                                 p (output, "{ ");
485                                 int top = a.GetUpperBound (0);
486                                 for (int i = a.GetLowerBound (0); i <= top; i++){
487                                         PrettyPrint (output, a.GetValue (i));
488                                         if (i != top)
489                                                 p (output, ", ");
490                                 }
491                                 p (output, " }");
492                         } else if (result is bool){
493                                 if ((bool) result)
494                                         p (output, "true");
495                                 else
496                                         p (output, "false");
497                         } else if (result is string){
498                                 p (output, String.Format ("\"{0}\"", EscapeString ((string)result)));
499                         } else if (result is IDictionary){
500                                 IDictionary dict = (IDictionary) result;
501                                 int top = dict.Count, count = 0;
502                                 
503                                 p (output, "{");
504                                 foreach (DictionaryEntry entry in dict){
505                                         count++;
506                                         p (output, "{ ");
507                                         PrettyPrint (output, entry.Key);
508                                         p (output, ", ");
509                                         PrettyPrint (output, entry.Value);
510                                         if (count != top)
511                                                 p (output, " }, ");
512                                         else
513                                                 p (output, " }");
514                                 }
515                                 p (output, "}");
516                         } else if (WorksAsEnumerable (result)) {
517                                 int i = 0;
518                                 p (output, "{ ");
519                                 foreach (object item in (IEnumerable) result) {
520                                         if (i++ != 0)
521                                                 p (output, ", ");
522
523                                         PrettyPrint (output, item);
524                                 }
525                                 p (output, " }");
526                         } else if (result is char) {
527                                 EscapeChar (output, (char) result);
528                         } else {
529                                 p (output, result.ToString ());
530                         }
531                 }
532
533                 public virtual int Run (string [] startup_files)
534                 {
535                         this.startup_files = startup_files;
536                         return ReadEvalPrintLoop ();
537                 }
538                 
539         }
540
541         //
542         // Stream helper extension methods
543         //
544         public static class StreamHelper {
545                 static DataConverter converter = DataConverter.LittleEndian;
546                 
547                 static void GetBuffer (this Stream stream, byte [] b)
548                 {
549                         int n, offset = 0;
550                         int len = b.Length;
551
552                         do {
553                                 n = stream.Read (b, offset, len);
554                                 if (n == 0)
555                                         throw new IOException ("End reached");
556
557                                 offset += n;
558                                 len -= n;
559                         } while (len > 0);
560                 }
561
562                 public static int GetInt (this Stream stream)
563                 {
564                         byte [] b = new byte [4];
565                         stream.GetBuffer (b);
566                         return converter.GetInt32 (b, 0);
567                 }
568
569                 public static string GetString (this Stream stream)
570                 {
571                         int len = stream.GetInt ();
572                         if (len == 0)
573                                 return "";
574
575                         byte [] b = new byte [len];
576                         stream.GetBuffer (b);
577
578                         return Encoding.UTF8.GetString (b);
579                 }
580
581                 public static void WriteInt (this Stream stream, int n)
582                 {
583                         byte [] bytes = converter.GetBytes (n);
584                         stream.Write (bytes, 0, bytes.Length);
585                 }
586         
587                 public static void WriteString (this Stream stream, string s)
588                 {
589                         stream.WriteInt (s.Length);
590                         byte [] bytes = Encoding.UTF8.GetBytes (s);
591                         stream.Write (bytes, 0, bytes.Length);
592                 }
593         }
594         
595         public enum AgentStatus : byte {
596                 // Received partial input, complete
597                 PARTIAL_INPUT  = 1,
598         
599                 // The result was set, expect the string with the result
600                 RESULT_SET     = 2,
601         
602                 // No result was set, complete
603                 RESULT_NOT_SET = 3,
604         
605                 // Errors and warnings string follows
606                 ERROR          = 4, 
607         }
608
609         class ClientCSharpShell : CSharpShell {
610                 string target_host;
611                 int target_port;
612                 
613                 public ClientCSharpShell (Evaluator evaluator, string target_host, int target_port) : base (evaluator)
614                 {
615                         this.target_port = target_port;
616                         this.target_host = target_host;
617                 }
618
619                 T ConnectServer<T> (Func<NetworkStream,T> callback, Action<Exception> error)
620                 {
621                         try {
622                                 var client = new TcpClient (target_host, target_port);
623                                 var ns = client.GetStream ();
624                                 T ret = callback (ns);
625                                 ns.Flush ();
626                                 ns.Close ();
627                                 client.Close ();
628                                 return ret;
629                         } catch (Exception e){
630                                 error (e);
631                                 return default(T);
632                         }
633                 }
634                 
635                 protected override string Evaluate (string input)
636                 {
637                         return ConnectServer<string> ((ns)=> {
638                                 try {
639                                         ns.WriteString ("EVALTXT");
640                                         ns.WriteString (input);
641
642                                         while (true) {
643                                                 AgentStatus s = (AgentStatus) ns.ReadByte ();
644                                         
645                                                 switch (s){
646                                                 case AgentStatus.PARTIAL_INPUT:
647                                                         return input;
648                                                 
649                                                 case AgentStatus.ERROR:
650                                                         string err = ns.GetString ();
651                                                         Console.Error.WriteLine (err);
652                                                         break;
653                                                 
654                                                 case AgentStatus.RESULT_NOT_SET:
655                                                         return null;
656                                                 
657                                                 case AgentStatus.RESULT_SET:
658                                                         string res = ns.GetString ();
659                                                         Console.WriteLine (res);
660                                                         return null;
661                                                 }
662                                         }
663                                 } catch (Exception e){
664                                         Console.Error.WriteLine ("Error evaluating expression, exception: {0}", e);
665                                 }
666                                 return null;
667                         }, (e) => {
668                                 Console.Error.WriteLine ("Error communicating with server {0}", e);
669                         });
670                 }
671                 
672                 public override int Run (string [] startup_files)
673                 {
674                         // The difference is that we do not call Evaluator.Init, that is done on the target
675                         this.startup_files = startup_files;
676                         return ReadEvalPrintLoop ();
677                 }
678         
679                 protected override void ConsoleInterrupt (object sender, ConsoleCancelEventArgs a)
680                 {
681                         ConnectServer<int> ((ns)=> {
682                                 ns.WriteString ("INTERRUPT");
683                                 return 0;
684                         }, (e) => { });
685                 }
686                         
687         }
688
689 #if !ON_DOTNET
690         //
691         // A shell connected to a CSharpAgent running in a remote process.
692         //  - maybe add 'class_name' and 'method_name' arguments to LoadAgent.
693         //  - Support Gtk and Winforms main loops if detected, this should
694         //    probably be done as a separate agent in a separate place.
695         //
696         class ClientCSharpShell_v1 : CSharpShell {
697                 NetworkStream ns, interrupt_stream;
698                 
699                 public ClientCSharpShell_v1 (Evaluator evaluator, int pid)
700                         : base (evaluator)
701                 {
702                         // Create a server socket we listen on whose address is passed to the agent
703                         TcpListener listener = new TcpListener (new IPEndPoint (IPAddress.Loopback, 0));
704                         listener.Start ();
705                         TcpListener interrupt_listener = new TcpListener (new IPEndPoint (IPAddress.Loopback, 0));
706                         interrupt_listener.Start ();
707         
708                         string agent_assembly = typeof (ClientCSharpShell).Assembly.Location;
709                         string agent_arg = String.Format ("--agent:{0}:{1}" ,
710                                                           ((IPEndPoint)listener.Server.LocalEndPoint).Port,
711                                                           ((IPEndPoint)interrupt_listener.Server.LocalEndPoint).Port);
712         
713                         var vm = new Attach.VirtualMachine (pid);
714                         vm.Attach (agent_assembly, agent_arg);
715         
716                         /* Wait for the client to connect */
717                         TcpClient client = listener.AcceptTcpClient ();
718                         ns = client.GetStream ();
719                         TcpClient interrupt_client = interrupt_listener.AcceptTcpClient ();
720                         interrupt_stream = interrupt_client.GetStream ();
721         
722                         Console.WriteLine ("Connected.");
723                 }
724
725                 //
726                 // A remote version of Evaluate
727                 //
728                 protected override string Evaluate (string input)
729                 {
730                         ns.WriteString (input);
731                         while (true) {
732                                 AgentStatus s = (AgentStatus) ns.ReadByte ();
733         
734                                 switch (s){
735                                 case AgentStatus.PARTIAL_INPUT:
736                                         return input;
737         
738                                 case AgentStatus.ERROR:
739                                         string err = ns.GetString ();
740                                         Console.Error.WriteLine (err);
741                                         break;
742         
743                                 case AgentStatus.RESULT_NOT_SET:
744                                         return null;
745         
746                                 case AgentStatus.RESULT_SET:
747                                         string res = ns.GetString ();
748                                         Console.WriteLine (res);
749                                         return null;
750                                 }
751                         }
752                 }
753                 
754                 public override int Run (string [] startup_files)
755                 {
756                         // The difference is that we do not call Evaluator.Init, that is done on the target
757                         this.startup_files = startup_files;
758                         return ReadEvalPrintLoop ();
759                 }
760         
761                 protected override void ConsoleInterrupt (object sender, ConsoleCancelEventArgs a)
762                 {
763                         // Do not about our program
764                         a.Cancel = true;
765         
766                         interrupt_stream.WriteByte (0);
767                         int c = interrupt_stream.ReadByte ();
768                         if (c != -1)
769                                 Console.WriteLine ("Execution interrupted");
770                 }
771                         
772         }
773
774         //
775         // This is the agent loaded into the target process when using --attach.
776         //
777         class CSharpAgent
778         {
779                 NetworkStream interrupt_stream;
780                 readonly Evaluator evaluator;
781                 TextWriter stderr;
782                 
783                 public CSharpAgent (Evaluator evaluator, String arg, TextWriter stderr)
784                 {
785                         this.evaluator = evaluator;
786                         this.stderr = stderr;
787                         new Thread (new ParameterizedThreadStart (Run)).Start (arg);
788                 }
789
790                 public void InterruptListener ()
791                 {
792                         while (true){
793                                 int b = interrupt_stream.ReadByte();
794                                 if (b == -1)
795                                         return;
796                                 evaluator.Interrupt ();
797                                 interrupt_stream.WriteByte (0);
798                         }
799                 }
800                 
801                 public void Run (object o)
802                 {
803                         string arg = (string)o;
804                         string ports = arg.Substring (8);
805                         int sp = ports.IndexOf (':');
806                         int port = Int32.Parse (ports.Substring (0, sp));
807                         int interrupt_port = Int32.Parse (ports.Substring (sp+1));
808         
809                         Console.WriteLine ("csharp-agent: started, connecting to localhost:" + port);
810         
811                         TcpClient client = new TcpClient ("127.0.0.1", port);
812                         TcpClient interrupt_client = new TcpClient ("127.0.0.1", interrupt_port);
813                         Console.WriteLine ("csharp-agent: connected.");
814         
815                         NetworkStream s = client.GetStream ();
816                         interrupt_stream = interrupt_client.GetStream ();
817                         new Thread (InterruptListener).Start ();
818
819                         try {
820                                 // Add all assemblies loaded later
821                                 AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoaded;
822         
823                                 // Add all currently loaded assemblies
824                                 foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
825                                         // Some assemblies seem to be already loaded, and loading them again causes 'defined multiple times' errors
826                                         if (a.GetName ().Name != "mscorlib" && a.GetName ().Name != "System.Core" && a.GetName ().Name != "System")
827                                                 evaluator.ReferenceAssembly (a);
828                                 }
829         
830                                 RunRepl (s);
831                         } finally {
832                                 AppDomain.CurrentDomain.AssemblyLoad -= AssemblyLoaded;
833                                 client.Close ();
834                                 interrupt_client.Close ();
835                                 Console.WriteLine ("csharp-agent: disconnected.");                      
836                         }
837                 }
838         
839                 void AssemblyLoaded (object sender, AssemblyLoadEventArgs e)
840                 {
841                         evaluator.ReferenceAssembly (e.LoadedAssembly);
842                 }
843         
844                 public void RunRepl (NetworkStream s)
845                 {
846                         string input = null;
847
848                         while (!InteractiveBase.QuitRequested) {
849                                 try {
850                                         string error_string;
851                                         StringWriter error_output = (StringWriter)stderr;
852
853                                         string line = s.GetString ();
854         
855                                         bool result_set;
856                                         object result;
857         
858                                         if (input == null)
859                                                 input = line;
860                                         else
861                                                 input = input + "\n" + line;
862         
863                                         try {
864                                                 input = evaluator.Evaluate (input, out result, out result_set);
865                                         } catch (Exception e) {
866                                                 s.WriteByte ((byte) AgentStatus.ERROR);
867                                                 s.WriteString (e.ToString ());
868                                                 s.WriteByte ((byte) AgentStatus.RESULT_NOT_SET);
869                                                 continue;
870                                         }
871                                         
872                                         if (input != null){
873                                                 s.WriteByte ((byte) AgentStatus.PARTIAL_INPUT);
874                                                 continue;
875                                         }
876         
877                                         // Send warnings and errors back
878                                         error_string = error_output.ToString ();
879                                         if (error_string.Length != 0){
880                                                 s.WriteByte ((byte) AgentStatus.ERROR);
881                                                 s.WriteString (error_output.ToString ());
882                                                 error_output.GetStringBuilder ().Clear ();
883                                         }
884         
885                                         if (result_set){
886                                                 s.WriteByte ((byte) AgentStatus.RESULT_SET);
887                                                 StringWriter sr = new StringWriter ();
888                                                 CSharpShell.PrettyPrint (sr, result);
889                                                 s.WriteString (sr.ToString ());
890                                         } else {
891                                                 s.WriteByte ((byte) AgentStatus.RESULT_NOT_SET);
892                                         }
893                                 } catch (IOException) {
894                                         break;
895                                 } catch (Exception e){
896                                         Console.WriteLine (e);
897                                 }
898                         }
899                 }
900         }
901
902         public class UnixUtils {
903                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
904                 extern static int _isatty (int fd);
905                         
906                 public static bool isatty (int fd)
907                 {
908                         try {
909                                 return _isatty (fd) == 1;
910                         } catch {
911                                 return false;
912                         }
913                 }
914         }
915 #endif
916 }
917         
918 namespace Mono.Management
919 {
920         interface IVirtualMachine {
921                 void LoadAgent (string filename, string args);
922         }
923 }
924