5 // Dietmar Maurer (dietmar@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // (C) 2004,2005 Novell, Inc. (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Security.Permissions;
46 private static TextWriter stdout;
47 private static TextWriter stderr;
48 private static TextReader stdin;
53 Encoding.InternalCodePage (ref code_page);
56 if (((int) Environment.Platform) == 128){
58 // On Unix systems (128), do not output the
59 // UTF-8 ZWNBSP (zero-width non-breaking space).
61 if (code_page == UTF8Encoding.UTF8_CODE_PAGE || ((code_page & 0x10000000) != 0))
62 encoding = Encoding.UTF8Unmarked;
64 encoding = Encoding.Default;
67 // On Windows, follow the Windows tradition
69 encoding = Encoding.Default;
72 stderr = new UnexceptionalStreamWriter (OpenStandardError (0), encoding);
73 ((StreamWriter)stderr).AutoFlush = true;
74 stderr = TextWriter.Synchronized (stderr, true);
76 stdout = new UnexceptionalStreamWriter (OpenStandardOutput (0), encoding);
77 ((StreamWriter)stdout).AutoFlush = true;
78 stdout = TextWriter.Synchronized (stdout, true);
80 stdin = new UnexceptionalStreamReader (OpenStandardInput (0), encoding);
81 stdin = TextReader.Synchronized (stdin);
90 public static TextWriter Error {
96 public static TextWriter Out {
102 public static TextReader In {
108 public static Stream OpenStandardError ()
110 return OpenStandardError (0);
113 // calling any FileStream constructor with an handle normally
114 // requires permissions UnmanagedCode permissions. In this
115 // case we assert this permission so the console can be used
116 // in partial trust (i.e. without having UnmanagedCode).
117 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
118 public static Stream OpenStandardError (int bufferSize)
121 return new FileStream (MonoIO.ConsoleError, FileAccess.Write, false, bufferSize, false, bufferSize == 0);
122 } catch (IOException) {
123 return new NullStream ();
127 public static Stream OpenStandardInput ()
129 return OpenStandardInput (0);
132 // calling any FileStream constructor with an handle normally
133 // requires permissions UnmanagedCode permissions. In this
134 // case we assert this permission so the console can be used
135 // in partial trust (i.e. without having UnmanagedCode).
136 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
137 public static Stream OpenStandardInput (int bufferSize)
140 return new FileStream (MonoIO.ConsoleInput, FileAccess.Read, false, bufferSize, false, bufferSize == 0);
141 } catch (IOException) {
142 return new NullStream ();
146 public static Stream OpenStandardOutput ()
148 return OpenStandardOutput (0);
151 // calling any FileStream constructor with an handle normally
152 // requires permissions UnmanagedCode permissions. In this
153 // case we assert this permission so the console can be used
154 // in partial trust (i.e. without having UnmanagedCode).
155 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
156 public static Stream OpenStandardOutput (int bufferSize)
159 return new FileStream (MonoIO.ConsoleOutput, FileAccess.Write, false, bufferSize, false, bufferSize == 0);
160 } catch (IOException) {
161 return new NullStream ();
165 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
166 public static void SetError (TextWriter newError)
168 if (newError == null)
169 throw new ArgumentNullException ("newError");
174 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
175 public static void SetIn (TextReader newIn)
178 throw new ArgumentNullException ("newIn");
183 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
184 public static void SetOut (TextWriter newOut)
187 throw new ArgumentNullException ("newOut");
192 public static void Write (bool value)
194 stdout.Write (value);
197 public static void Write (char value)
199 stdout.Write (value);
202 public static void Write (char[] value)
204 stdout.Write (value);
207 public static void Write (decimal value)
209 stdout.Write (value);
212 public static void Write (double value)
214 stdout.Write (value);
217 public static void Write (int value)
219 stdout.Write (value);
222 public static void Write (long value)
224 stdout.Write (value);
227 public static void Write (object value)
229 stdout.Write (value);
232 public static void Write (float value)
234 stdout.Write (value);
237 public static void Write (string value)
239 stdout.Write (value);
242 [CLSCompliant (false)]
243 public static void Write (uint value)
245 stdout.Write (value);
248 [CLSCompliant (false)]
249 public static void Write (ulong value)
251 stdout.Write (value);
254 public static void Write (string format, object arg0)
256 stdout.Write (format, arg0);
259 public static void Write (string format, params object[] arg)
261 stdout.Write (format, arg);
264 public static void Write (char[] buffer, int index, int count)
266 stdout.Write (buffer, index, count);
269 public static void Write (string format, object arg0, object arg1)
271 stdout.Write (format, arg0, arg1);
274 public static void Write (string format, object arg0, object arg1, object arg2 )
276 stdout.Write (format, arg0, arg1, arg2);
279 #if ! BOOTSTRAP_WITH_OLDLIB
280 [CLSCompliant (false)]
281 public static void Write (string format, object arg0, object arg1, object arg2, object arg3, __arglist)
283 ArgIterator iter = new ArgIterator (__arglist);
284 int argCount = iter.GetRemainingCount();
286 object[] args = new object [argCount + 4];
291 for (int i = 0; i < argCount; i++) {
292 TypedReference typedRef = iter.GetNextArg ();
293 args [i + 4] = TypedReference.ToObject (typedRef);
296 stdout.Write (String.Format (format, args));
300 public static void WriteLine ()
305 public static void WriteLine (bool value)
307 stdout.WriteLine (value);
310 public static void WriteLine (char value)
312 stdout.WriteLine (value);
315 public static void WriteLine (char[] value)
317 stdout.WriteLine (value);
320 public static void WriteLine (decimal value)
322 stdout.WriteLine (value);
325 public static void WriteLine (double value)
327 stdout.WriteLine (value);
330 public static void WriteLine (int value)
332 stdout.WriteLine (value);
335 public static void WriteLine (long value)
337 stdout.WriteLine (value);
340 public static void WriteLine (object value)
342 stdout.WriteLine (value);
345 public static void WriteLine (float value)
347 stdout.WriteLine (value);
350 public static void WriteLine (string value)
352 stdout.WriteLine (value);
355 [CLSCompliant (false)]
356 public static void WriteLine (uint value)
358 stdout.WriteLine (value);
361 [CLSCompliant (false)]
362 public static void WriteLine (ulong value)
364 stdout.WriteLine (value);
367 public static void WriteLine (string format, object arg0)
369 stdout.WriteLine (format, arg0);
372 public static void WriteLine (string format, params object[] arg)
374 stdout.WriteLine (format, arg);
377 public static void WriteLine (char[] buffer, int index, int count)
379 stdout.WriteLine (buffer, index, count);
382 public static void WriteLine (string format, object arg0, object arg1)
384 stdout.WriteLine (format, arg0, arg1);
387 public static void WriteLine (string format, object arg0, object arg1, object arg2)
389 stdout.WriteLine (format, arg0, arg1, arg2);
392 #if ! BOOTSTRAP_WITH_OLDLIB
393 [CLSCompliant (false)]
394 public static void WriteLine (string format, object arg0, object arg1, object arg2, object arg3, __arglist)
396 ArgIterator iter = new ArgIterator (__arglist);
397 int argCount = iter.GetRemainingCount();
399 object[] args = new object [argCount + 4];
404 for (int i = 0; i < argCount; i++) {
405 TypedReference typedRef = iter.GetNextArg ();
406 args [i + 4] = TypedReference.ToObject (typedRef);
409 stdout.WriteLine (String.Format (format, args));
413 public static int Read ()
415 return stdin.Read ();
418 public static string ReadLine ()
422 if (ConsoleDriver.Initialized) {
423 prevEcho = ConsoleDriver.Echo;
424 ConsoleDriver.Echo = true;
427 return stdin.ReadLine ();
429 if (ConsoleDriver.Initialized)
430 ConsoleDriver.Echo = prevEcho;
435 // On windows, for en-US the Default is Windows-1252, while input/output is IBM437
436 // We might want to initialize these fields in the ConsoleDriver instead.
437 static Encoding inputEncoding = Encoding.Default;
438 static Encoding outputEncoding = Encoding.Default;
440 public static Encoding InputEncoding {
441 get { return inputEncoding; }
442 set { inputEncoding = value; }
445 public static Encoding OutputEncoding {
446 get { return outputEncoding; }
447 set { outputEncoding = value; }
450 public static ConsoleColor BackgroundColor {
451 get { return ConsoleDriver.BackgroundColor; }
452 set { ConsoleDriver.BackgroundColor = value; }
455 public static int BufferHeight {
456 get { return ConsoleDriver.BufferHeight; }
457 set { ConsoleDriver.BufferHeight = value; }
460 public static int BufferWidth {
461 get { return ConsoleDriver.BufferWidth; }
462 set { ConsoleDriver.BufferWidth = value; }
465 public static int CursorLeft {
466 get { return ConsoleDriver.CursorLeft; }
467 set { ConsoleDriver.CursorLeft = value; }
470 public static int CursorTop {
471 get { return ConsoleDriver.CursorTop; }
472 set { ConsoleDriver.CursorTop = value; }
475 public static bool CursorVisible {
476 get { return ConsoleDriver.CursorVisible; }
477 set { ConsoleDriver.CursorVisible = value; }
480 public static ConsoleColor ForegroundColor {
481 get { return ConsoleDriver.ForegroundColor; }
482 set { ConsoleDriver.ForegroundColor = value; }
485 public static bool KeyAvailable {
486 get { return ConsoleDriver.KeyAvailable; }
489 public static string Title {
490 get { return ConsoleDriver.Title; }
491 set { ConsoleDriver.Title = value; }
494 public static bool TreatControlCAsInput {
495 get { return ConsoleDriver.TreatControlCAsInput; }
496 set { ConsoleDriver.TreatControlCAsInput = value; }
499 public static int WindowHeight {
500 get { return ConsoleDriver.WindowHeight; }
501 set { ConsoleDriver.WindowHeight = value; }
504 public static int WindowLeft {
505 get { return ConsoleDriver.WindowLeft; }
506 set { ConsoleDriver.WindowLeft = value; }
509 public static int WindowTop {
510 get { return ConsoleDriver.WindowTop; }
511 set { ConsoleDriver.WindowTop = value; }
514 public static int WindowWidth {
515 get { return ConsoleDriver.WindowWidth; }
516 set { ConsoleDriver.WindowWidth = value; }
519 public static void Beep ()
524 public static void Beep (int frequency, int duration)
526 if (frequency < 37 || frequency > 32767)
527 throw new ArgumentOutOfRangeException ("frequency");
530 throw new ArgumentOutOfRangeException ("duration");
532 ConsoleDriver.Beep (frequency, duration);
535 public static void Clear ()
537 ConsoleDriver.Clear ();
541 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
542 int targetLeft, int targetTop)
544 throw new NotImplementedException ();
548 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
549 int targetLeft, int targetTop, Char sourceChar,
550 ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
552 throw new NotImplementedException ();
555 public static ConsoleKeyInfo ReadKey ()
557 return ReadKey (false);
560 public static ConsoleKeyInfo ReadKey (bool intercept)
562 return ConsoleDriver.ReadKey (intercept);
565 public static void ResetColor ()
567 ConsoleDriver.ResetColor ();
570 public static void SetBufferSize (int width, int height)
572 ConsoleDriver.SetBufferSize (width, height);
575 public static void SetCursorPosition (int left, int top)
577 ConsoleDriver.SetCursorPosition (left, top);
580 public static void SetWindowPosition (int left, int top)
582 ConsoleDriver.SetWindowPosition (left, top);
585 public static void SetWindowSize (int width, int height)
587 ConsoleDriver.SetWindowSize (width, height);
591 //public event ConsoleCancelEventHandler CancelKeyPress;