copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / corlib / System / Console.cs
1 //
2 // System.Console.cs
3 //
4 // Author:
5 //      Dietmar Maurer (dietmar@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) 2004,2005 Novell, Inc. (http://www.novell.com)
10 //
11
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System.IO;
33 using System.Security.Permissions;
34 using System.Text;
35
36 namespace System
37 {
38         public
39 #if NET_2_0
40         static
41 #else
42         sealed
43 #endif
44         class Console
45         {
46                 private static TextWriter stdout;
47                 private static TextWriter stderr;
48                 private static TextReader stdin;
49
50                 static Console ()
51                 {
52                         int code_page = 0;
53                         Encoding.InternalCodePage (ref code_page);
54                         Encoding encoding;
55
56                         if (((int) Environment.Platform) == 128){
57                                 //
58                                 // On Unix systems (128), do not output the
59                                 // UTF-8 ZWNBSP (zero-width non-breaking space).
60                                 //
61                                 if (code_page == UTF8Encoding.UTF8_CODE_PAGE || ((code_page & 0x10000000) != 0))
62                                         encoding = Encoding.UTF8Unmarked;
63                                 else
64                                         encoding = Encoding.Default;
65                         } else {
66                                 //
67                                 // On Windows, follow the Windows tradition
68                                 //
69                                 encoding = Encoding.Default;
70                         }
71
72                         stderr = new UnexceptionalStreamWriter (OpenStandardError (0), encoding); 
73                         ((StreamWriter)stderr).AutoFlush = true;
74                         stderr = TextWriter.Synchronized (stderr, true);
75
76                         stdout = new UnexceptionalStreamWriter (OpenStandardOutput (0), encoding);
77                         ((StreamWriter)stdout).AutoFlush = true;
78                         stdout = TextWriter.Synchronized (stdout, true);
79
80                         stdin  = new UnexceptionalStreamReader (OpenStandardInput (0), encoding);
81                         stdin = TextReader.Synchronized (stdin);
82                 }
83
84 #if !NET_2_0
85                 private Console ()
86                 {
87                 }
88 #endif
89
90                 public static TextWriter Error {
91                         get {
92                                 return stderr;
93                         }
94                 }
95
96                 public static TextWriter Out {
97                         get {
98                                 return stdout;
99                         }
100                 }
101
102                 public static TextReader In {
103                         get {
104                                 return stdin;
105                         }
106                 }
107
108                 public static Stream OpenStandardError ()
109                 {
110                         return OpenStandardError (0);
111                 }
112
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)
119                 {
120                         try {
121                                 return new FileStream (MonoIO.ConsoleError, FileAccess.Write, false, bufferSize, false, bufferSize == 0);
122                         } catch (IOException) {
123                                 return new NullStream ();
124                         }
125                 }
126
127                 public static Stream OpenStandardInput ()
128                 {
129                         return OpenStandardInput (0);
130                 }
131
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)
138                 {
139                         try {
140                                 return new FileStream (MonoIO.ConsoleInput, FileAccess.Read, false, bufferSize, false, bufferSize == 0);
141                         } catch (IOException) {
142                                 return new NullStream ();
143                         }
144                 }
145
146                 public static Stream OpenStandardOutput ()
147                 {
148                         return OpenStandardOutput (0);
149                 }
150
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)
157                 {
158                         try {
159                                 return new FileStream (MonoIO.ConsoleOutput, FileAccess.Write, false, bufferSize, false, bufferSize == 0);
160                         } catch (IOException) {
161                                 return new NullStream ();
162                         }
163                 }
164
165                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
166                 public static void SetError (TextWriter newError)
167                 {
168                         if (newError == null)
169                                 throw new ArgumentNullException ("newError");
170
171                         stderr = newError;
172                 }
173
174                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
175                 public static void SetIn (TextReader newIn)
176                 {
177                         if (newIn == null)
178                                 throw new ArgumentNullException ("newIn");
179
180                         stdin = newIn;
181                 }
182
183                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
184                 public static void SetOut (TextWriter newOut)
185                 {
186                         if (newOut == null)
187                                 throw new ArgumentNullException ("newOut");
188
189                         stdout = newOut;
190                 }
191
192                 public static void Write (bool value)
193                 {
194                         stdout.Write (value);
195                 }
196
197                 public static void Write (char value)
198                 {
199                         stdout.Write (value);
200                 }
201
202                 public static void Write (char[] value)
203                 {
204                         stdout.Write (value);
205                 }
206
207                 public static void Write (decimal value)
208                 {
209                         stdout.Write (value);
210                 }
211                 
212                 public static void Write (double value)
213                 {
214                         stdout.Write (value);
215                 }
216
217                 public static void Write (int value)
218                 {
219                         stdout.Write (value);
220                 }
221
222                 public static void Write (long value)
223                 {
224                         stdout.Write (value);
225                 }
226
227                 public static void Write (object value)
228                 {
229                         stdout.Write (value);
230                 }
231
232                 public static void Write (float value)
233                 {
234                         stdout.Write (value);
235                 }
236
237                 public static void Write (string value)
238                 {
239                         stdout.Write (value);
240                 }
241
242                 [CLSCompliant (false)]
243                 public static void Write (uint value)
244                 {
245                         stdout.Write (value);
246                 }
247
248                 [CLSCompliant (false)]
249                 public static void Write (ulong value)
250                 {
251                         stdout.Write (value);
252                 }
253
254                 public static void Write (string format, object arg0)
255                 {
256                         stdout.Write (format, arg0);
257                 }
258
259                 public static void Write (string format, params object[] arg)
260                 {
261                         stdout.Write (format, arg);
262                 }
263
264                 public static void Write (char[] buffer, int index, int count)
265                 {
266                         stdout.Write (buffer, index, count);
267                 }
268
269                 public static void Write (string format, object arg0, object arg1)
270                 {
271                         stdout.Write (format, arg0, arg1);
272                 }
273
274                 public static void Write (string format, object arg0, object arg1, object arg2 )
275                 {
276                         stdout.Write (format, arg0, arg1, arg2);
277                 }
278
279 #if ! BOOTSTRAP_WITH_OLDLIB
280                 [CLSCompliant (false)]
281                 public static void Write (string format, object arg0, object arg1, object arg2, object arg3, __arglist)
282                 {
283                         ArgIterator iter = new ArgIterator (__arglist);
284                         int argCount = iter.GetRemainingCount();
285
286                         object[] args = new object [argCount + 4];
287                         args [0] = arg0;
288                         args [1] = arg1;
289                         args [2] = arg2;
290                         args [3] = arg3;
291                         for (int i = 0; i < argCount; i++) {
292                                 TypedReference typedRef = iter.GetNextArg ();
293                                 args [i + 4] = TypedReference.ToObject (typedRef);
294                         }
295
296                         stdout.Write (String.Format (format, args));
297                 }
298 #endif
299
300                 public static void WriteLine ()
301                 {
302                         stdout.WriteLine ();
303                 }
304
305                 public static void WriteLine (bool value)
306                 {
307                         stdout.WriteLine (value);
308                 }
309
310                 public static void WriteLine (char value)
311                 {
312                         stdout.WriteLine (value);
313                 }
314
315                 public static void WriteLine (char[] value)
316                 {
317                         stdout.WriteLine (value);
318                 }
319
320                 public static void WriteLine (decimal value)
321                 {
322                         stdout.WriteLine (value);
323                 }
324
325                 public static void WriteLine (double value)
326                 {
327                         stdout.WriteLine (value);
328                 }
329
330                 public static void WriteLine (int value)
331                 {
332                         stdout.WriteLine (value);
333                 }
334
335                 public static void WriteLine (long value)
336                 {
337                         stdout.WriteLine (value);
338                 }
339
340                 public static void WriteLine (object value)
341                 {
342                         stdout.WriteLine (value);
343                 }
344
345                 public static void WriteLine (float value)
346                 {
347                         stdout.WriteLine (value);
348                 }
349
350                 public static void WriteLine (string value)
351                 {
352                         stdout.WriteLine (value);
353                 }
354
355                 [CLSCompliant (false)]
356                 public static void WriteLine (uint value)
357                 {
358                         stdout.WriteLine (value);
359                 }
360
361                 [CLSCompliant (false)]
362                 public static void WriteLine (ulong value)
363                 {
364                         stdout.WriteLine (value);
365                 }
366
367                 public static void WriteLine (string format, object arg0)
368                 {
369                         stdout.WriteLine (format, arg0);
370                 }
371
372                 public static void WriteLine (string format, params object[] arg)
373                 {
374                         stdout.WriteLine (format, arg);
375                 }
376
377                 public static void WriteLine (char[] buffer, int index, int count)
378                 {
379                         stdout.WriteLine (buffer, index, count);
380                 }
381
382                 public static void WriteLine (string format, object arg0, object arg1)
383                 {
384                         stdout.WriteLine (format, arg0, arg1);
385                 }
386
387                 public static void WriteLine (string format, object arg0, object arg1, object arg2)
388                 {
389                         stdout.WriteLine (format, arg0, arg1, arg2);
390                 }
391
392 #if ! BOOTSTRAP_WITH_OLDLIB
393                 [CLSCompliant (false)]
394                 public static void WriteLine (string format, object arg0, object arg1, object arg2, object arg3, __arglist)
395                 {
396                         ArgIterator iter = new ArgIterator (__arglist);
397                         int argCount = iter.GetRemainingCount();
398
399                         object[] args = new object [argCount + 4];
400                         args [0] = arg0;
401                         args [1] = arg1;
402                         args [2] = arg2;
403                         args [3] = arg3;
404                         for (int i = 0; i < argCount; i++) {
405                                 TypedReference typedRef = iter.GetNextArg ();
406                                 args [i + 4] = TypedReference.ToObject (typedRef);
407                         }
408
409                         stdout.WriteLine (String.Format (format, args));
410                 }
411 #endif
412
413                 public static int Read ()
414                 {
415                         return stdin.Read ();
416                 }
417
418                 public static string ReadLine ()
419                 {
420 #if NET_2_0
421                         bool prevEcho;
422                         if (ConsoleDriver.Initialized) {
423                                 prevEcho = ConsoleDriver.Echo;
424                                 ConsoleDriver.Echo = true;
425                         }
426 #endif
427                         return stdin.ReadLine ();
428 #if NET_2_0
429                         if (ConsoleDriver.Initialized)
430                                 ConsoleDriver.Echo = prevEcho;
431 #endif
432                 }
433
434 #if NET_2_0
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;
439
440                 public static Encoding InputEncoding {
441                         get { return inputEncoding; }
442                         set { inputEncoding = value; }
443                 }
444
445                 public static Encoding OutputEncoding {
446                         get { return outputEncoding; }
447                         set { outputEncoding = value; }
448                 }
449
450                 public static ConsoleColor BackgroundColor {
451                         get { return ConsoleDriver.BackgroundColor; }
452                         set { ConsoleDriver.BackgroundColor = value; }
453                 }
454
455                 public static int BufferHeight {
456                         get { return ConsoleDriver.BufferHeight; }
457                         set { ConsoleDriver.BufferHeight = value; }
458                 }
459
460                 public static int BufferWidth {
461                         get { return ConsoleDriver.BufferWidth; }
462                         set { ConsoleDriver.BufferWidth = value; }
463                 }
464
465                 public static int CursorLeft {
466                         get { return ConsoleDriver.CursorLeft; }
467                         set { ConsoleDriver.CursorLeft = value; }
468                 }
469
470                 public static int CursorTop {
471                         get { return ConsoleDriver.CursorTop; }
472                         set { ConsoleDriver.CursorTop = value; }
473                 }
474
475                 public static bool CursorVisible {
476                         get { return ConsoleDriver.CursorVisible; }
477                         set { ConsoleDriver.CursorVisible = value; }
478                 }
479
480                 public static ConsoleColor ForegroundColor {
481                         get { return ConsoleDriver.ForegroundColor; }
482                         set { ConsoleDriver.ForegroundColor = value; }
483                 }
484
485                 public static bool KeyAvailable {
486                         get { return ConsoleDriver.KeyAvailable; }
487                 }
488
489                 public static string Title {
490                         get { return ConsoleDriver.Title; }
491                         set { ConsoleDriver.Title = value; }
492                 }
493
494                 public static bool TreatControlCAsInput {
495                         get { return ConsoleDriver.TreatControlCAsInput; }
496                         set { ConsoleDriver.TreatControlCAsInput = value; }
497                 }
498
499                 public static int WindowHeight {
500                         get { return ConsoleDriver.WindowHeight; }
501                         set { ConsoleDriver.WindowHeight = value; }
502                 }
503
504                 public static int WindowLeft {
505                         get { return ConsoleDriver.WindowLeft; }
506                         set { ConsoleDriver.WindowLeft = value; }
507                 }
508
509                 public static int WindowTop {
510                         get { return ConsoleDriver.WindowTop; }
511                         set { ConsoleDriver.WindowTop = value; }
512                 }
513
514                 public static int WindowWidth {
515                         get { return ConsoleDriver.WindowWidth; }
516                         set { ConsoleDriver.WindowWidth = value; }
517                 }
518
519                 public static void Beep ()
520                 {
521                         Beep (1000, 500);
522                 }
523
524                 public static void Beep (int frequency, int duration)
525                 {
526                         if (frequency < 37 || frequency > 32767)
527                                 throw new ArgumentOutOfRangeException ("frequency");
528
529                         if (duration <= 0)
530                                 throw new ArgumentOutOfRangeException ("duration");
531
532                         ConsoleDriver.Beep (frequency, duration);
533                 }
534
535                 public static void Clear ()
536                 {
537                         ConsoleDriver.Clear ();
538                 }
539
540                 [MonoTODO]
541                 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
542                                                 int targetLeft, int targetTop)
543                 {
544                         throw new NotImplementedException ();
545                 }
546
547                 [MonoTODO]
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)
551                 {
552                         throw new NotImplementedException ();
553                 }
554
555                 public static ConsoleKeyInfo ReadKey ()
556                 {
557                         return ReadKey (false);
558                 }
559
560                 public static ConsoleKeyInfo ReadKey (bool intercept)
561                 {
562                         return ConsoleDriver.ReadKey (intercept);
563                 }
564
565                 public static void ResetColor ()
566                 {
567                         ConsoleDriver.ResetColor ();
568                 }
569
570                 public static void SetBufferSize (int width, int height)
571                 {
572                         ConsoleDriver.SetBufferSize (width, height);
573                 }
574
575                 public static void SetCursorPosition (int left, int top)
576                 {
577                         ConsoleDriver.SetCursorPosition (left, top);
578                 }
579
580                 public static void SetWindowPosition (int left, int top)
581                 {
582                         ConsoleDriver.SetWindowPosition (left, top);
583                 }
584
585                 public static void SetWindowSize (int width, int height)
586                 {
587                         ConsoleDriver.SetWindowSize (width, height);
588                 }
589
590                 //FIXME
591                 //public event ConsoleCancelEventHandler CancelKeyPress;
592 #endif
593         }
594 }
595