OSDN Git Service

PR target/50678
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-textio.adb
index 7afb804..721deca 100644 (file)
@@ -1,43 +1,44 @@
 ------------------------------------------------------------------------------
 --                                                                          --
---                         GNAT RUNTIME COMPONENTS                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
 --                                                                          --
 --                          A D A . T E X T _ I O                           --
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2004 Free Software Foundation, Inc.          --
+--          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
--- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
--- for  more details.  You should have  received  a copy of the GNU General --
--- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
 --                                                                          --
--- As a special exception,  if other files  instantiate  generics from this --
--- unit, or you link  this unit with other files  to produce an executable, --
--- this  unit  does not  by itself cause  the resulting  executable  to  be --
--- covered  by the  GNU  General  Public  License.  This exception does not --
--- however invalidate  any other reasons why  the executable file  might be --
--- covered by the  GNU Public License.                                      --
+-- As a special exception under Section 7 of GPL version 3, you are granted --
+-- additional permissions described in the GCC Runtime Library Exception,   --
+-- version 3.1, as published by the Free Software Foundation.               --
+--                                                                          --
+-- You should have received a copy of the GNU General Public License and    --
+-- a copy of the GCC Runtime Library Exception along with this program;     --
+-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
+-- <http://www.gnu.org/licenses/>.                                          --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
 --                                                                          --
 ------------------------------------------------------------------------------
 
-with Ada.Streams;          use Ada.Streams;
-with Interfaces.C_Streams; use Interfaces.C_Streams;
-with System;
+with Ada.Streams;             use Ada.Streams;
+with Interfaces.C_Streams;    use Interfaces.C_Streams;
+
 with System.File_IO;
 with System.CRTL;
-with Unchecked_Conversion;
-with Unchecked_Deallocation;
+with System.WCh_Cnv;          use System.WCh_Cnv;
+with System.WCh_Con;          use System.WCh_Con;
+
+with Ada.Unchecked_Conversion;
+with Ada.Unchecked_Deallocation;
 
 pragma Elaborate_All (System.File_IO);
 --  Needed because of calls to Chain_File in package body elaboration
@@ -48,19 +49,102 @@ package body Ada.Text_IO is
 
    subtype AP is FCB.AFCB_Ptr;
 
-   function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
-   function To_TIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
+   function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
+   function To_TIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
    use type FCB.File_Mode;
 
    use type System.CRTL.size_t;
 
+   WC_Encoding : Character;
+   pragma Import (C, WC_Encoding, "__gl_wc_encoding");
+   --  Default wide character encoding
+
+   Err_Name : aliased String := "*stderr" & ASCII.NUL;
+   In_Name  : aliased String := "*stdin" & ASCII.NUL;
+   Out_Name : aliased String := "*stdout" & ASCII.NUL;
+   --  Names of standard files
+   --
+   --  Use "preallocated" strings to avoid calling "new" during the elaboration
+   --  of the run time. This is needed in the tasking case to avoid calling
+   --  Task_Lock too early. A filename is expected to end with a null character
+   --  in the runtime, here the null characters are added just to have a
+   --  correct filename length.
+   --
+   --  Note: the names for these files are bogus, and probably it would be
+   --  better for these files to have no names, but the ACVC tests insist!
+   --  We use names that are bound to fail in open etc.
+
+   Null_Str : aliased constant String := "";
+   --  Used as form string for standard files
+
+   -----------------------
+   -- Local Subprograms --
+   -----------------------
+
+   function Get_Upper_Half_Char
+     (C    : Character;
+      File : File_Type) return Character;
+   --  This function is shared by Get and Get_Immediate to extract an encoded
+   --  upper half character value from the given File. The first byte has
+   --  already been read and is passed in C. The character value is returned as
+   --  the result, and the file pointer is bumped past the character.
+   --  Constraint_Error is raised if the encoded value is outside the bounds of
+   --  type Character.
+
+   function Get_Upper_Half_Char_Immed
+     (C    : Character;
+      File : File_Type) return Character;
+   --  This routine is identical to Get_Upper_Half_Char, except that the reads
+   --  are done in Get_Immediate mode (i.e. without waiting for a line return).
+
+   function Getc (File : File_Type) return int;
+   --  Gets next character from file, which has already been checked for being
+   --  in read status, and returns the character read if no error occurs. The
+   --  result is EOF if the end of file was read.
+
+   function Getc_Immed (File : File_Type) return int;
+   --  This routine is identical to Getc, except that the read is done in
+   --  Get_Immediate mode (i.e. without waiting for a line return).
+
+   function Has_Upper_Half_Character (Item : String) return Boolean;
+   --  Returns True if any of the characters is in the range 16#80#-16#FF#
+
+   function Nextc (File : File_Type) return int;
+   --  Returns next character from file without skipping past it (i.e. it is a
+   --  combination of Getc followed by an Ungetc).
+
+   procedure Put_Encoded (File : File_Type; Char : Character);
+   --  Called to output a character Char to the given File, when the encoding
+   --  method for the file is other than brackets, and Char is upper half.
+
+   procedure Putc (ch : int; File : File_Type);
+   --  Outputs the given character to the file, which has already been checked
+   --  for being in output status. Device_Error is raised if the character
+   --  cannot be written.
+
+   procedure Set_WCEM (File : in out File_Type);
+   --  Called by Open and Create to set the wide character encoding method for
+   --  the file, processing a WCEM form parameter if one is present. File is
+   --  IN OUT because it may be closed in case of an error.
+
+   procedure Terminate_Line (File : File_Type);
+   --  If the file is in Write_File or Append_File mode, and the current line
+   --  is not terminated, then a line terminator is written using New_Line.
+   --  Note that there is no Terminate_Page routine, because the page mark at
+   --  the end of the file is implied if necessary.
+
+   procedure Ungetc (ch : int; File : File_Type);
+   --  Pushes back character into stream, using ungetc. The caller has checked
+   --  that the file is in read status. Device_Error is raised if the character
+   --  cannot be pushed back. An attempt to push back and end of file character
+   --  (EOF) is ignored.
+
    -------------------
    -- AFCB_Allocate --
    -------------------
 
    function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr is
       pragma Unreferenced (Control_Block);
-
    begin
       return new Text_AFCB;
    end AFCB_Allocate;
@@ -69,7 +153,7 @@ package body Ada.Text_IO is
    -- AFCB_Close --
    ----------------
 
-   procedure AFCB_Close (File : access Text_AFCB) is
+   procedure AFCB_Close (File : not null access Text_AFCB) is
    begin
       --  If the file being closed is one of the current files, then close
       --  the corresponding current file. It is not clear that this action
@@ -91,11 +175,11 @@ package body Ada.Text_IO is
    -- AFCB_Free --
    ---------------
 
-   procedure AFCB_Free (File : access Text_AFCB) is
+   procedure AFCB_Free (File : not null access Text_AFCB) is
       type FCB_Ptr is access all Text_AFCB;
       FT : FCB_Ptr := FCB_Ptr (File);
 
-      procedure Free is new Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
+      procedure Free is new Ada.Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
 
    begin
       Free (FT);
@@ -107,7 +191,7 @@ package body Ada.Text_IO is
 
    procedure Close (File : in out File_Type) is
    begin
-      FIO.Close (AP (File));
+      FIO.Close (AP (File)'Unrestricted_Access);
    end Close;
 
    ---------
@@ -118,7 +202,7 @@ package body Ada.Text_IO is
    --  to exceed the value of Count'Last, i.e. no check is required for
    --  overflow raising layout error.
 
-   function Col (File : in File_Type) return Positive_Count is
+   function Col (File : File_Type) return Positive_Count is
    begin
       FIO.Check_File_Open (AP (File));
       return File.Col;
@@ -135,9 +219,9 @@ package body Ada.Text_IO is
 
    procedure Create
      (File : in out File_Type;
-      Mode : in File_Mode := Out_File;
-      Name : in String := "";
-      Form : in String := "")
+      Mode : File_Mode := Out_File;
+      Name : String := "";
+      Form : String := "")
    is
       Dummy_File_Control_Block : Text_AFCB;
       pragma Warnings (Off, Dummy_File_Control_Block);
@@ -155,6 +239,7 @@ package body Ada.Text_IO is
                 Text      => True);
 
       File.Self := File;
+      Set_WCEM (File);
    end Create;
 
    -------------------
@@ -205,21 +290,23 @@ package body Ada.Text_IO is
 
    procedure Delete (File : in out File_Type) is
    begin
-      FIO.Delete (AP (File));
+      FIO.Delete (AP (File)'Unrestricted_Access);
    end Delete;
 
    -----------------
    -- End_Of_File --
    -----------------
 
-   function End_Of_File (File : in File_Type) return Boolean is
-      ch  : int;
+   function End_Of_File (File : File_Type) return Boolean is
+      ch : int;
 
    begin
       FIO.Check_Read_Status (AP (File));
 
-      if File.Before_LM then
+      if File.Before_Upper_Half_Character then
+         return False;
 
+      elsif File.Before_LM then
          if File.Before_LM_PM then
             return Nextc (File) = EOF;
          end if;
@@ -270,13 +357,16 @@ package body Ada.Text_IO is
    -- End_Of_Line --
    -----------------
 
-   function End_Of_Line (File : in File_Type) return Boolean is
+   function End_Of_Line (File : File_Type) return Boolean is
       ch : int;
 
    begin
       FIO.Check_Read_Status (AP (File));
 
-      if File.Before_LM then
+      if File.Before_Upper_Half_Character then
+         return False;
+
+      elsif File.Before_LM then
          return True;
 
       else
@@ -301,7 +391,7 @@ package body Ada.Text_IO is
    -- End_Of_Page --
    -----------------
 
-   function End_Of_Page (File : in File_Type) return Boolean is
+   function End_Of_Page (File : File_Type) return Boolean is
       ch  : int;
 
    begin
@@ -310,6 +400,9 @@ package body Ada.Text_IO is
       if not File.Is_Regular_File then
          return False;
 
+      elsif File.Before_Upper_Half_Character then
+         return False;
+
       elsif File.Before_LM then
          if File.Before_LM_PM then
             return True;
@@ -357,7 +450,7 @@ package body Ada.Text_IO is
    -- Flush --
    -----------
 
-   procedure Flush (File : in File_Type) is
+   procedure Flush (File : File_Type) is
    begin
       FIO.Flush (AP (File));
    end Flush;
@@ -371,7 +464,7 @@ package body Ada.Text_IO is
    -- Form --
    ----------
 
-   function Form (File : in File_Type) return String is
+   function Form (File : File_Type) return String is
    begin
       return FIO.Form (AP (File));
    end Form;
@@ -381,7 +474,7 @@ package body Ada.Text_IO is
    ---------
 
    procedure Get
-     (File : in File_Type;
+     (File : File_Type;
       Item : out Character)
    is
       ch : int;
@@ -389,7 +482,11 @@ package body Ada.Text_IO is
    begin
       FIO.Check_Read_Status (AP (File));
 
-      if File.Before_LM then
+      if File.Before_Upper_Half_Character then
+         File.Before_Upper_Half_Character := False;
+         Item := File.Saved_Upper_Half_Character;
+
+      elsif File.Before_LM then
          File.Before_LM := False;
          File.Col := 1;
 
@@ -430,7 +527,7 @@ package body Ada.Text_IO is
    end Get;
 
    procedure Get
-     (File : in File_Type;
+     (File : File_Type;
       Item : out String)
    is
       ch : int;
@@ -486,40 +583,36 @@ package body Ada.Text_IO is
    -- Get_Immediate --
    -------------------
 
-   --  More work required here ???
-
    procedure Get_Immediate
-     (File : in File_Type;
+     (File : File_Type;
       Item : out Character)
    is
       ch          : int;
-      end_of_file : int;
-
-      procedure getc_immediate
-        (stream      : FILEs;
-         ch          : out int;
-         end_of_file : out int);
-      pragma Import (C, getc_immediate, "getc_immediate");
 
    begin
       FIO.Check_Read_Status (AP (File));
 
-      if File.Before_LM then
+      if File.Before_Upper_Half_Character then
+         File.Before_Upper_Half_Character := False;
+         Item := File.Saved_Upper_Half_Character;
+
+      elsif File.Before_LM then
          File.Before_LM := False;
          File.Before_LM_PM := False;
-         ch := LM;
+         Item := Character'Val (LM);
 
       else
-         getc_immediate (File.Stream, ch, end_of_file);
+         ch := Getc_Immed (File);
 
-         if ferror (File.Stream) /= 0 then
-            raise Device_Error;
-         elsif end_of_file /= 0 then
+         if ch = EOF then
             raise End_Error;
+         else
+            Item :=
+              (if not Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
+               then Character'Val (ch)
+               else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
          end if;
       end if;
-
-      Item := Character'Val (ch);
    end Get_Immediate;
 
    procedure Get_Immediate
@@ -530,7 +623,7 @@ package body Ada.Text_IO is
    end Get_Immediate;
 
    procedure Get_Immediate
-     (File      : in File_Type;
+     (File      : File_Type;
       Item      : out Character;
       Available : out Boolean)
    is
@@ -547,19 +640,17 @@ package body Ada.Text_IO is
 
    begin
       FIO.Check_Read_Status (AP (File));
+      Available := True;
 
-      --  If we are logically before an end of line, but physically after it,
-      --  then we just return the end of line character, no I/O is necessary.
+      if File.Before_Upper_Half_Character then
+         File.Before_Upper_Half_Character := False;
+         Item := File.Saved_Upper_Half_Character;
 
-      if File.Before_LM then
+      elsif File.Before_LM then
          File.Before_LM := False;
          File.Before_LM_PM := False;
-
-         Available := True;
          Item := Character'Val (LM);
 
-      --  Normal case where a read operation is required
-
       else
          getc_immediate_nowait (File.Stream, ch, end_of_file, avail);
 
@@ -575,7 +666,11 @@ package body Ada.Text_IO is
 
          else
             Available := True;
-            Item := Character'Val (ch);
+
+            Item :=
+              (if Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
+               then Character'Val (ch)
+               else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
          end if;
       end if;
 
@@ -594,123 +689,159 @@ package body Ada.Text_IO is
    --------------
 
    procedure Get_Line
-     (File : in File_Type;
+     (File : File_Type;
       Item : out String;
+      Last : out Natural) is separate;
+   --  The implementation of Ada.Text_IO.Get_Line is split into a subunit so
+   --  that different implementations can be used on different systems. In
+   --  particular the standard implementation uses low level stuff that is
+   --  not appropriate for the JVM and .NET implementations.
+
+   procedure Get_Line
+     (Item : out String;
       Last : out Natural)
    is
-      ch : int;
-
    begin
-      FIO.Check_Read_Status (AP (File));
-      Last := Item'First - 1;
+      Get_Line (Current_In, Item, Last);
+   end Get_Line;
 
-      --  Immediate exit for null string, this is a case in which we do not
-      --  need to test for end of file and we do not skip a line mark under
-      --  any circumstances.
+   function Get_Line (File : File_Type) return String is
+      Buffer : String (1 .. 500);
+      Last   : Natural;
 
-      if Last >= Item'Last then
-         return;
-      end if;
+      function Get_Rest (S : String) return String;
+      --  This is a recursive function that reads the rest of the line and
+      --  returns it. S is the part read so far.
 
-      --  Here we have at least one character, if we are immediately before
-      --  a line mark, then we will just skip past it storing no characters.
+      --------------
+      -- Get_Rest --
+      --------------
 
-      if File.Before_LM then
-         File.Before_LM := False;
-         File.Before_LM_PM := False;
+      function Get_Rest (S : String) return String is
+
+         --  Each time we allocate a buffer the same size as what we have
+         --  read so far. This limits us to a logarithmic number of calls
+         --  to Get_Rest and also ensures only a linear use of stack space.
 
-      --  Otherwise we need to read some characters
+         Buffer : String (1 .. S'Length);
+         Last   : Natural;
 
+      begin
+         Get_Line (File, Buffer, Last);
+
+         declare
+            R : constant String := S & Buffer (1 .. Last);
+         begin
+            if Last < Buffer'Last then
+               return R;
+            else
+               return Get_Rest (R);
+            end if;
+         end;
+      end Get_Rest;
+
+   --  Start of processing for Get_Line
+
+   begin
+      Get_Line (File, Buffer, Last);
+
+      if Last < Buffer'Last then
+         return Buffer (1 .. Last);
       else
-         ch := Getc (File);
+         return Get_Rest (Buffer (1 .. Last));
+      end if;
+   end Get_Line;
+
+   function Get_Line return String is
+   begin
+      return Get_Line (Current_In);
+   end Get_Line;
+
+   -------------------------
+   -- Get_Upper_Half_Char --
+   -------------------------
+
+   function Get_Upper_Half_Char
+     (C    : Character;
+      File : File_Type) return Character
+   is
+      Result : Wide_Character;
+
+      function In_Char return Character;
+      --  Function used to obtain additional characters it the wide character
+      --  sequence is more than one character long.
 
-         --  If we are at the end of file now, it means we are trying to
-         --  skip a file terminator and we raise End_Error (RM A.10.7(20))
+      function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
 
+      -------------
+      -- In_Char --
+      -------------
+
+      function In_Char return Character is
+         ch : constant Integer := Getc (File);
+      begin
          if ch = EOF then
             raise End_Error;
+         else
+            return Character'Val (ch);
          end if;
+      end In_Char;
 
-         --  Loop through characters. Don't bother if we hit a page mark,
-         --  since in normal files, page marks can only follow line marks
-         --  in any case and we only promise to treat the page nonsense
-         --  correctly in the absense of such rogue page marks.
-
-         loop
-            --  Exit the loop if read is terminated by encountering line mark
-
-            exit when ch = LM;
-
-            --  Otherwise store the character, note that we know that ch is
-            --  something other than LM or EOF. It could possibly be a page
-            --  mark if there is a stray page mark in the middle of a line,
-            --  but this is not an official page mark in any case, since
-            --  official page marks can only follow a line mark. The whole
-            --  page business is pretty much nonsense anyway, so we do not
-            --  want to waste time trying to make sense out of non-standard
-            --  page marks in the file! This means that the behavior of
-            --  Get_Line is different from repeated Get of a character, but
-            --  that's too bad. We only promise that page numbers etc make
-            --  sense if the file is formatted in a standard manner.
-
-            --  Note: we do not adjust the column number because it is quicker
-            --  to adjust it once at the end of the operation than incrementing
-            --  it each time around the loop.
-
-            Last := Last + 1;
-            Item (Last) := Character'Val (ch);
-
-            --  All done if the string is full, this is the case in which
-            --  we do not skip the following line mark. We need to adjust
-            --  the column number in this case.
-
-            if Last = Item'Last then
-               File.Col := File.Col + Count (Item'Length);
-               return;
-            end if;
+   --  Start of processing for Get_Upper_Half_Char
 
-            --  Otherwise read next character. We also exit from the loop if
-            --  we read an end of file. This is the case where the last line
-            --  is not terminated with a line mark, and we consider that there
-            --  is an implied line mark in this case (this is a non-standard
-            --  file, but it is nice to treat it reasonably).
+   begin
+      Result := WC_In (C, File.WC_Method);
 
-            ch := Getc (File);
-            exit when ch = EOF;
-         end loop;
+      if Wide_Character'Pos (Result) > 16#FF# then
+         raise Constraint_Error with
+           "invalid wide character in Text_'I'O input";
+      else
+         return Character'Val (Wide_Character'Pos (Result));
       end if;
+   end Get_Upper_Half_Char;
 
-      --  We have skipped past, but not stored, a line mark. Skip following
-      --  page mark if one follows, but do not do this for a non-regular
-      --  file (since otherwise we get annoying wait for an extra character)
+   -------------------------------
+   -- Get_Upper_Half_Char_Immed --
+   -------------------------------
 
-      File.Line := File.Line + 1;
-      File.Col := 1;
+   function Get_Upper_Half_Char_Immed
+     (C    : Character;
+      File : File_Type) return Character
+   is
+      Result : Wide_Character;
 
-      if File.Before_LM_PM then
-         File.Line := 1;
-         File.Before_LM_PM := False;
-         File.Page := File.Page + 1;
+      function In_Char return Character;
+      --  Function used to obtain additional characters it the wide character
+      --  sequence is more than one character long.
 
-      elsif File.Is_Regular_File then
-         ch := Getc (File);
+      function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
 
-         if ch = PM and then File.Is_Regular_File then
-            File.Line := 1;
-            File.Page := File.Page + 1;
+      -------------
+      -- In_Char --
+      -------------
+
+      function In_Char return Character is
+         ch : constant Integer := Getc_Immed (File);
+      begin
+         if ch = EOF then
+            raise End_Error;
          else
-            Ungetc (ch, File);
+            return Character'Val (ch);
          end if;
-      end if;
-   end Get_Line;
+      end In_Char;
+
+   --  Start of processing for Get_Upper_Half_Char_Immed
 
-   procedure Get_Line
-     (Item : out String;
-      Last : out Natural)
-   is
    begin
-      Get_Line (Current_In, Item, Last);
-   end Get_Line;
+      Result := WC_In (C, File.WC_Method);
+
+      if Wide_Character'Pos (Result) > 16#FF# then
+         raise Constraint_Error with
+           "invalid wide character in Text_'I'O input";
+      else
+         return Character'Val (Wide_Character'Pos (Result));
+      end if;
+   end Get_Upper_Half_Char_Immed;
 
    ----------
    -- Getc --
@@ -729,11 +860,105 @@ package body Ada.Text_IO is
       end if;
    end Getc;
 
+   ----------------
+   -- Getc_Immed --
+   ----------------
+
+   function Getc_Immed (File : File_Type) return int is
+      ch          : int;
+      end_of_file : int;
+
+      procedure getc_immediate
+        (stream : FILEs; ch : out int; end_of_file : out int);
+      pragma Import (C, getc_immediate, "getc_immediate");
+
+   begin
+      FIO.Check_Read_Status (AP (File));
+
+      if File.Before_LM then
+         File.Before_LM := False;
+         File.Before_LM_PM := False;
+         ch := LM;
+
+      else
+         getc_immediate (File.Stream, ch, end_of_file);
+
+         if ferror (File.Stream) /= 0 then
+            raise Device_Error;
+         elsif end_of_file /= 0 then
+            return EOF;
+         end if;
+      end if;
+
+      return ch;
+   end Getc_Immed;
+
+   ------------------------------
+   -- Has_Upper_Half_Character --
+   ------------------------------
+
+   function Has_Upper_Half_Character (Item : String) return Boolean is
+   begin
+      for J in Item'Range loop
+         if Character'Pos (Item (J)) >= 16#80# then
+            return True;
+         end if;
+      end loop;
+
+      return False;
+   end Has_Upper_Half_Character;
+
+   -------------------------------
+   -- Initialize_Standard_Files --
+   -------------------------------
+
+   procedure Initialize_Standard_Files is
+   begin
+      Standard_Err.Stream            := stderr;
+      Standard_Err.Name              := Err_Name'Access;
+      Standard_Err.Form              := Null_Str'Unrestricted_Access;
+      Standard_Err.Mode              := FCB.Out_File;
+      Standard_Err.Is_Regular_File   := is_regular_file (fileno (stderr)) /= 0;
+      Standard_Err.Is_Temporary_File := False;
+      Standard_Err.Is_System_File    := True;
+      Standard_Err.Is_Text_File      := True;
+      Standard_Err.Access_Method     := 'T';
+      Standard_Err.Self              := Standard_Err;
+      Standard_Err.WC_Method         := Default_WCEM;
+
+      Standard_In.Stream             := stdin;
+      Standard_In.Name               := In_Name'Access;
+      Standard_In.Form               := Null_Str'Unrestricted_Access;
+      Standard_In.Mode               := FCB.In_File;
+      Standard_In.Is_Regular_File    := is_regular_file (fileno (stdin)) /= 0;
+      Standard_In.Is_Temporary_File  := False;
+      Standard_In.Is_System_File     := True;
+      Standard_In.Is_Text_File       := True;
+      Standard_In.Access_Method      := 'T';
+      Standard_In.Self               := Standard_In;
+      Standard_In.WC_Method          := Default_WCEM;
+
+      Standard_Out.Stream            := stdout;
+      Standard_Out.Name              := Out_Name'Access;
+      Standard_Out.Form              := Null_Str'Unrestricted_Access;
+      Standard_Out.Mode              := FCB.Out_File;
+      Standard_Out.Is_Regular_File   := is_regular_file (fileno (stdout)) /= 0;
+      Standard_Out.Is_Temporary_File := False;
+      Standard_Out.Is_System_File    := True;
+      Standard_Out.Is_Text_File      := True;
+      Standard_Out.Access_Method     := 'T';
+      Standard_Out.Self              := Standard_Out;
+      Standard_Out.WC_Method         := Default_WCEM;
+
+      FIO.Make_Unbuffered (AP (Standard_Out));
+      FIO.Make_Unbuffered (AP (Standard_Err));
+   end Initialize_Standard_Files;
+
    -------------
    -- Is_Open --
    -------------
 
-   function Is_Open (File : in File_Type) return Boolean is
+   function Is_Open (File : File_Type) return Boolean is
    begin
       return FIO.Is_Open (AP (File));
    end Is_Open;
@@ -746,7 +971,7 @@ package body Ada.Text_IO is
    --  to exceed the value of Count'Last, i.e. no check is required for
    --  overflow raising layout error.
 
-   function Line (File : in File_Type) return Positive_Count is
+   function Line (File : File_Type) return Positive_Count is
    begin
       FIO.Check_File_Open (AP (File));
       return File.Line;
@@ -761,7 +986,7 @@ package body Ada.Text_IO is
    -- Line_Length --
    -----------------
 
-   function Line_Length (File : in File_Type) return Count is
+   function Line_Length (File : File_Type) return Count is
    begin
       FIO.Check_Write_Status (AP (File));
       return File.Line_Length;
@@ -777,7 +1002,7 @@ package body Ada.Text_IO is
    ----------------
 
    procedure Look_Ahead
-     (File        : in File_Type;
+     (File        : File_Type;
       Item        : out Character;
       End_Of_Line : out Boolean)
    is
@@ -786,22 +1011,54 @@ package body Ada.Text_IO is
    begin
       FIO.Check_Read_Status (AP (File));
 
+      --  If we are logically before a line mark, we can return immediately
+
       if File.Before_LM then
          End_Of_Line := True;
          Item := ASCII.NUL;
 
+      --  If we are before an upper half character just return it (this can
+      --  happen if there are two calls to Look_Ahead in a row).
+
+      elsif File.Before_Upper_Half_Character then
+         End_Of_Line := False;
+         Item := File.Saved_Upper_Half_Character;
+
+      --  Otherwise we must read a character from the input stream
+
       else
-         ch := Nextc (File);
+         ch := Getc (File);
 
          if ch = LM
            or else ch = EOF
            or else (ch = PM and then File.Is_Regular_File)
          then
             End_Of_Line := True;
+            Ungetc (ch, File);
             Item := ASCII.NUL;
-         else
+
+         --  Case where character obtained does not represent the start of an
+         --  encoded sequence so it stands for itself and we can unget it with
+         --  no difficulty.
+
+         elsif not Is_Start_Of_Encoding
+                     (Character'Val (ch), File.WC_Method)
+         then
             End_Of_Line := False;
+            Ungetc (ch, File);
             Item := Character'Val (ch);
+
+         --  For the start of an encoding, we read the character using the
+         --  Get_Upper_Half_Char routine. It will occupy more than one byte
+         --  so we can't put it back with ungetc. Instead we save it in the
+         --  control block, setting a flag that everyone interested in reading
+         --  characters must test before reading the stream.
+
+         else
+            Item := Get_Upper_Half_Char (Character'Val (ch), File);
+            End_Of_Line := False;
+            File.Saved_Upper_Half_Character := Item;
+            File.Before_Upper_Half_Character := True;
          end if;
       end if;
    end Look_Ahead;
@@ -818,7 +1075,7 @@ package body Ada.Text_IO is
    -- Mode --
    ----------
 
-   function Mode (File : in File_Type) return File_Mode is
+   function Mode (File : File_Type) return File_Mode is
    begin
       return To_TIO (FIO.Mode (AP (File)));
    end Mode;
@@ -827,7 +1084,7 @@ package body Ada.Text_IO is
    -- Name --
    ----------
 
-   function Name (File : in File_Type) return String is
+   function Name (File : File_Type) return String is
    begin
       return FIO.Name (AP (File));
    end Name;
@@ -837,15 +1094,15 @@ package body Ada.Text_IO is
    --------------
 
    procedure New_Line
-     (File    : in File_Type;
-      Spacing : in Positive_Count := 1)
+     (File    : File_Type;
+      Spacing : Positive_Count := 1)
    is
    begin
       --  Raise Constraint_Error if out of range value. The reason for this
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if Spacing not in Positive_Count then
+      if not Spacing'Valid then
          raise Constraint_Error;
       end if;
 
@@ -867,7 +1124,7 @@ package body Ada.Text_IO is
       File.Col := 1;
    end New_Line;
 
-   procedure New_Line (Spacing : in Positive_Count := 1) is
+   procedure New_Line (Spacing : Positive_Count := 1) is
    begin
       New_Line (Current_Out, Spacing);
    end New_Line;
@@ -876,7 +1133,7 @@ package body Ada.Text_IO is
    -- New_Page --
    --------------
 
-   procedure New_Page (File : in File_Type) is
+   procedure New_Page (File : File_Type) is
    begin
       FIO.Check_Write_Status (AP (File));
 
@@ -925,9 +1182,9 @@ package body Ada.Text_IO is
 
    procedure Open
      (File : in out File_Type;
-      Mode : in File_Mode;
-      Name : in String;
-      Form : in String := "")
+      Mode : File_Mode;
+      Name : String;
+      Form : String := "")
    is
       Dummy_File_Control_Block : Text_AFCB;
       pragma Warnings (Off, Dummy_File_Control_Block);
@@ -945,6 +1202,7 @@ package body Ada.Text_IO is
                 Text      => True);
 
       File.Self := File;
+      Set_WCEM (File);
    end Open;
 
    ----------
@@ -955,7 +1213,7 @@ package body Ada.Text_IO is
    --  to exceed the value of Count'Last, i.e. no check is required for
    --  overflow raising layout error.
 
-   function Page (File : in File_Type) return Positive_Count is
+   function Page (File : File_Type) return Positive_Count is
    begin
       FIO.Check_File_Open (AP (File));
       return File.Page;
@@ -970,7 +1228,7 @@ package body Ada.Text_IO is
    -- Page_Length --
    -----------------
 
-   function Page_Length (File : in File_Type) return Count is
+   function Page_Length (File : File_Type) return Count is
    begin
       FIO.Check_Write_Status (AP (File));
       return File.Page_Length;
@@ -986,8 +1244,8 @@ package body Ada.Text_IO is
    ---------
 
    procedure Put
-     (File : in File_Type;
-      Item : in Character)
+     (File : File_Type;
+      Item : Character)
    is
    begin
       FIO.Check_Write_Status (AP (File));
@@ -996,14 +1254,25 @@ package body Ada.Text_IO is
          New_Line (File);
       end if;
 
-      if fputc (Character'Pos (Item), File.Stream) = EOF then
-         raise Device_Error;
+      --  If lower half character, or brackets encoding, output directly
+
+      if Character'Pos (Item) < 16#80#
+        or else File.WC_Method = WCEM_Brackets
+      then
+         if fputc (Character'Pos (Item), File.Stream) = EOF then
+            raise Device_Error;
+         end if;
+
+      --  Case of upper half character with non-brackets encoding
+
+      else
+         Put_Encoded (File, Item);
       end if;
 
       File.Col := File.Col + 1;
    end Put;
 
-   procedure Put (Item : in Character) is
+   procedure Put (Item : Character) is
    begin
       FIO.Check_Write_Status (AP (Current_Out));
 
@@ -1013,8 +1282,19 @@ package body Ada.Text_IO is
          New_Line (Current_Out);
       end if;
 
-      if fputc (Character'Pos (Item), Current_Out.Stream) = EOF then
-         raise Device_Error;
+      --  If lower half character, or brackets encoding, output directly
+
+      if Character'Pos (Item) < 16#80#
+        or else Default_WCEM = WCEM_Brackets
+      then
+         if fputc (Character'Pos (Item), Current_Out.Stream) = EOF then
+            raise Device_Error;
+         end if;
+
+      --  Case of upper half character with non-brackets encoding
+
+      else
+         Put_Encoded (Current_Out, Item);
       end if;
 
       Current_Out.Col := Current_Out.Col + 1;
@@ -1025,18 +1305,24 @@ package body Ada.Text_IO is
    ---------
 
    procedure Put
-     (File : in File_Type;
-      Item : in String)
+     (File : File_Type;
+      Item : String)
    is
    begin
       FIO.Check_Write_Status (AP (File));
 
+      --  Only have something to do if string is non-null
+
       if Item'Length > 0 then
 
-         --  If we have bounded lines, then do things character by
-         --  character (this seems a rare case anyway!)
+         --  If we have bounded lines, or if the file encoding is other than
+         --  Brackets and the string has at least one upper half character,
+         --  then output the string character by character.
 
-         if File.Line_Length /= 0 then
+         if File.Line_Length /= 0
+           or else (File.WC_Method /= WCEM_Brackets
+                      and then Has_Upper_Half_Character (Item))
+         then
             for J in Item'Range loop
                Put (File, Item (J));
             end loop;
@@ -1052,18 +1338,43 @@ package body Ada.Text_IO is
       end if;
    end Put;
 
-   procedure Put (Item : in String) is
+   procedure Put (Item : String) is
    begin
       Put (Current_Out, Item);
    end Put;
 
+   -----------------
+   -- Put_Encoded --
+   -----------------
+
+   procedure Put_Encoded (File : File_Type; Char : Character) is
+      procedure Out_Char (C : Character);
+      --  Procedure to output one character of an upper half encoded sequence
+
+      procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
+
+      --------------
+      -- Out_Char --
+      --------------
+
+      procedure Out_Char (C : Character) is
+      begin
+         Putc (Character'Pos (C), File);
+      end Out_Char;
+
+   --  Start of processing for Put_Encoded
+
+   begin
+      WC_Out (Wide_Character'Val (Character'Pos (Char)), File.WC_Method);
+   end Put_Encoded;
+
    --------------
    -- Put_Line --
    --------------
 
    procedure Put_Line
-     (File : in File_Type;
-      Item : in String)
+     (File : File_Type;
+      Item : String)
    is
       Ilen   : Natural := Item'Length;
       Istart : Natural := Item'First;
@@ -1071,16 +1382,24 @@ package body Ada.Text_IO is
    begin
       FIO.Check_Write_Status (AP (File));
 
-      --  If we have bounded lines, then just do a put and a new line. In
-      --  this case we will end up doing things character by character in
-      --  any case, and it is a rare situation.
+      --  If we have bounded lines, or if the file encoding is other than
+      --  Brackets and the string has at least one upper half character, then
+      --  output the string character by character.
+
+      if File.Line_Length /= 0
+        or else (File.WC_Method /= WCEM_Brackets
+                   and then Has_Upper_Half_Character (Item))
+      then
+         for J in Item'Range loop
+            Put (File, Item (J));
+         end loop;
 
-      if File.Line_Length /= 0 then
-         Put (File, Item);
          New_Line (File);
          return;
       end if;
 
+      --  Normal case where we do not need to output character by character
+
       --  We setup a single string that has the necessary terminators and
       --  then write it with a single call. The reason for doing this is
       --  that it gives better behavior for the use of Put_Line in multi-
@@ -1127,7 +1446,7 @@ package body Ada.Text_IO is
       end;
    end Put_Line;
 
-   procedure Put_Line (Item : in String) is
+   procedure Put_Line (Item : String) is
    begin
       Put_Line (Current_Out, Item);
    end Put_Line;
@@ -1159,6 +1478,8 @@ package body Ada.Text_IO is
       pragma Warnings (Off, Discard_ch);
 
    begin
+      --  Need to deal with Before_Upper_Half_Character ???
+
       if File.Mode /= FCB.In_File then
          raise Mode_Error;
       end if;
@@ -1231,13 +1552,13 @@ package body Ada.Text_IO is
 
    procedure Reset
      (File : in out File_Type;
-      Mode : in File_Mode)
+      Mode : File_Mode)
    is
    begin
       --  Don't allow change of mode for current file (RM A.10.2(5))
 
-      if (File = Current_In or else
-          File = Current_Out  or else
+      if (File = Current_In  or else
+          File = Current_Out or else
           File = Current_Error)
         and then To_FCB (Mode) /= File.Mode
       then
@@ -1245,7 +1566,7 @@ package body Ada.Text_IO is
       end if;
 
       Terminate_Line (File);
-      FIO.Reset (AP (File), To_FCB (Mode));
+      FIO.Reset (AP (File)'Unrestricted_Access, To_FCB (Mode));
       File.Page := 1;
       File.Line := 1;
       File.Col  := 1;
@@ -1258,7 +1579,7 @@ package body Ada.Text_IO is
    procedure Reset (File : in out File_Type) is
    begin
       Terminate_Line (File);
-      FIO.Reset (AP (File));
+      FIO.Reset (AP (File)'Unrestricted_Access);
       File.Page := 1;
       File.Line := 1;
       File.Col  := 1;
@@ -1273,8 +1594,8 @@ package body Ada.Text_IO is
    -------------
 
    procedure Set_Col
-     (File : in File_Type;
-      To   : in Positive_Count)
+     (File : File_Type;
+      To   : Positive_Count)
    is
       ch : int;
 
@@ -1283,49 +1604,88 @@ package body Ada.Text_IO is
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if To not in Positive_Count then
+      if not To'Valid then
          raise Constraint_Error;
       end if;
 
       FIO.Check_File_Open (AP (File));
 
-      if To = File.Col then
-         return;
-      end if;
+      --  Output case
 
       if Mode (File) >= Out_File then
+
+         --  Error if we attempt to set Col to a value greater than the
+         --  maximum permissible line length.
+
          if File.Line_Length /= 0 and then To > File.Line_Length then
             raise Layout_Error;
          end if;
 
+         --  If we are behind current position, then go to start of new line
+
          if To < File.Col then
             New_Line (File);
          end if;
 
+         --  Loop to output blanks till we are at the required column
+
          while File.Col < To loop
             Put (File, ' ');
          end loop;
 
+      --  Input case
+
       else
+         --  If we are logically before a LM, but physically after it, the
+         --  file position still reflects the position before the LM, so eat
+         --  it now and adjust the file position appropriately.
+
+         if File.Before_LM then
+            File.Before_LM := False;
+            File.Before_LM_PM := False;
+            File.Line := File.Line + 1;
+            File.Col := 1;
+         end if;
+
+         --  Loop reading characters till we get one at the required Col value
+
          loop
+            --  Read next character. The reason we have to read ahead is to
+            --  skip formatting characters, the effect of Set_Col is to set
+            --  us to a real character with the right Col value, and format
+            --  characters don't count.
+
             ch := Getc (File);
 
+            --  Error if we hit an end of file
+
             if ch = EOF then
                raise End_Error;
 
+            --  If line mark, eat it and adjust file position
+
             elsif ch = LM then
                File.Line := File.Line + 1;
                File.Col := 1;
 
+            --  If recognized page mark, eat it, and adjust file position
+
             elsif ch = PM and then File.Is_Regular_File then
                File.Page := File.Page + 1;
                File.Line := 1;
                File.Col := 1;
 
+            --  Otherwise this is the character we are looking for, so put it
+            --  back in the input stream (we have not adjusted the file
+            --  position yet, so everything is set right after this ungetc).
+
             elsif To = File.Col then
                Ungetc (ch, File);
                return;
 
+            --  Keep skipping characters if we are not there yet, updating the
+            --  file position past the skipped character.
+
             else
                File.Col := File.Col + 1;
             end if;
@@ -1333,7 +1693,7 @@ package body Ada.Text_IO is
       end if;
    end Set_Col;
 
-   procedure Set_Col (To : in Positive_Count) is
+   procedure Set_Col (To : Positive_Count) is
    begin
       Set_Col (Current_Out, To);
    end Set_Col;
@@ -1342,7 +1702,7 @@ package body Ada.Text_IO is
    -- Set_Error --
    ---------------
 
-   procedure Set_Error (File : in File_Type) is
+   procedure Set_Error (File : File_Type) is
    begin
       FIO.Check_Write_Status (AP (File));
       Current_Err := File;
@@ -1352,7 +1712,7 @@ package body Ada.Text_IO is
    -- Set_Input --
    ---------------
 
-   procedure Set_Input (File : in File_Type) is
+   procedure Set_Input (File : File_Type) is
    begin
       FIO.Check_Read_Status (AP (File));
       Current_In := File;
@@ -1363,15 +1723,15 @@ package body Ada.Text_IO is
    --------------
 
    procedure Set_Line
-     (File : in File_Type;
-      To   : in Positive_Count)
+     (File : File_Type;
+      To   : Positive_Count)
    is
    begin
       --  Raise Constraint_Error if out of range value. The reason for this
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if To not in Positive_Count then
+      if not To'Valid then
          raise Constraint_Error;
       end if;
 
@@ -1401,7 +1761,7 @@ package body Ada.Text_IO is
       end if;
    end Set_Line;
 
-   procedure Set_Line (To : in Positive_Count) is
+   procedure Set_Line (To : Positive_Count) is
    begin
       Set_Line (Current_Out, To);
    end Set_Line;
@@ -1410,13 +1770,13 @@ package body Ada.Text_IO is
    -- Set_Line_Length --
    ---------------------
 
-   procedure Set_Line_Length (File : in File_Type; To : in Count) is
+   procedure Set_Line_Length (File : File_Type; To : Count) is
    begin
       --  Raise Constraint_Error if out of range value. The reason for this
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if To not in Count then
+      if not To'Valid then
          raise Constraint_Error;
       end if;
 
@@ -1424,7 +1784,7 @@ package body Ada.Text_IO is
       File.Line_Length := To;
    end Set_Line_Length;
 
-   procedure Set_Line_Length (To : in Count) is
+   procedure Set_Line_Length (To : Count) is
    begin
       Set_Line_Length (Current_Out, To);
    end Set_Line_Length;
@@ -1433,7 +1793,7 @@ package body Ada.Text_IO is
    -- Set_Output --
    ----------------
 
-   procedure Set_Output (File : in File_Type) is
+   procedure Set_Output (File : File_Type) is
    begin
       FIO.Check_Write_Status (AP (File));
       Current_Out := File;
@@ -1443,13 +1803,13 @@ package body Ada.Text_IO is
    -- Set_Page_Length --
    ---------------------
 
-   procedure Set_Page_Length (File : in File_Type; To : in Count) is
+   procedure Set_Page_Length (File : File_Type; To : Count) is
    begin
       --  Raise Constraint_Error if out of range value. The reason for this
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if To not in Count then
+      if not To'Valid then
          raise Constraint_Error;
       end if;
 
@@ -1457,18 +1817,48 @@ package body Ada.Text_IO is
       File.Page_Length := To;
    end Set_Page_Length;
 
-   procedure Set_Page_Length (To : in Count) is
+   procedure Set_Page_Length (To : Count) is
    begin
       Set_Page_Length (Current_Out, To);
    end Set_Page_Length;
 
+   --------------
+   -- Set_WCEM --
+   --------------
+
+   procedure Set_WCEM (File : in out File_Type) is
+      Start : Natural;
+      Stop  : Natural;
+
+   begin
+      File.WC_Method := WCEM_Brackets;
+      FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
+
+      if Start = 0 then
+         File.WC_Method := WCEM_Brackets;
+
+      else
+         if Stop = Start then
+            for J in WC_Encoding_Letters'Range loop
+               if File.Form (Start) = WC_Encoding_Letters (J) then
+                  File.WC_Method := J;
+                  return;
+               end if;
+            end loop;
+         end if;
+
+         Close (File);
+         raise Use_Error with "invalid WCEM form parameter";
+      end if;
+   end Set_WCEM;
+
    ---------------
    -- Skip_Line --
    ---------------
 
    procedure Skip_Line
-     (File    : in File_Type;
-      Spacing : in Positive_Count := 1)
+     (File    : File_Type;
+      Spacing : Positive_Count := 1)
    is
       ch : int;
 
@@ -1477,7 +1867,7 @@ package body Ada.Text_IO is
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if Spacing not in Positive_Count then
+      if not Spacing'Valid then
          raise Constraint_Error;
       end if;
 
@@ -1486,7 +1876,12 @@ package body Ada.Text_IO is
       for L in 1 .. Spacing loop
          if File.Before_LM then
             File.Before_LM := False;
-            File.Before_LM_PM := False;
+
+            --  Note that if File.Before_LM_PM is currently set, we also have
+            --  to reset it (because it makes sense for Before_LM_PM to be set
+            --  only when Before_LM is also set). This is done later on in this
+            --  subprogram, as soon as Before_LM_PM has been taken into account
+            --  for the purpose of page and line counts.
 
          else
             ch := Getc (File);
@@ -1511,7 +1906,7 @@ package body Ada.Text_IO is
             --  up for such files, so we assume an implicit LM in this case.
 
             loop
-               exit when ch = LM or ch = EOF;
+               exit when ch = LM or else ch = EOF;
                ch := Getc (File);
             end loop;
          end if;
@@ -1544,11 +1939,12 @@ package body Ada.Text_IO is
                Ungetc (ch, File);
             end if;
          end if;
-
       end loop;
+
+      File.Before_Upper_Half_Character := False;
    end Skip_Line;
 
-   procedure Skip_Line (Spacing : in Positive_Count := 1) is
+   procedure Skip_Line (Spacing : Positive_Count := 1) is
    begin
       Skip_Line (Current_In, Spacing);
    end Skip_Line;
@@ -1557,7 +1953,7 @@ package body Ada.Text_IO is
    -- Skip_Page --
    ---------------
 
-   procedure Skip_Page (File : in File_Type) is
+   procedure Skip_Page (File : File_Type) is
       ch : int;
 
    begin
@@ -1606,6 +2002,7 @@ package body Ada.Text_IO is
       File.Page := File.Page + 1;
       File.Line := 1;
       File.Col  := 1;
+      File.Before_Upper_Half_Character := False;
    end Skip_Page;
 
    procedure Skip_Page is
@@ -1712,8 +2109,10 @@ package body Ada.Text_IO is
 
    procedure Write
      (File : in out Text_AFCB;
-      Item : in Stream_Element_Array)
+      Item : Stream_Element_Array)
    is
+      pragma Warnings (Off, File);
+      --  Because in this implementation we don't need IN OUT, we only read
 
       function Has_Translated_Characters return Boolean;
       --  return True if Item array contains a character which will be
@@ -1721,11 +2120,16 @@ package body Ada.Text_IO is
       --  character under DOS based systems which is character 10.
 
       text_translation_required : Boolean;
+      for text_translation_required'Size use Character'Size;
       pragma Import (C, text_translation_required,
                      "__gnat_text_translation_required");
 
       Siz : constant size_t := Item'Length;
 
+      -------------------------------
+      -- Has_Translated_Characters --
+      -------------------------------
+
       function Has_Translated_Characters return Boolean is
       begin
          for K in Item'Range loop
@@ -1737,7 +2141,10 @@ package body Ada.Text_IO is
       end Has_Translated_Characters;
 
       Needs_Binary_Write : constant Boolean :=
-        text_translation_required and then Has_Translated_Characters;
+                             text_translation_required
+                               and then Has_Translated_Characters;
+
+   --  Start of processing for Write
 
    begin
       if File.Mode = FCB.In_File then
@@ -1757,7 +2164,6 @@ package body Ada.Text_IO is
       --  with text mode if needed.
 
       if Needs_Binary_Write then
-
          if fflush (File.Stream) = -1 then
             raise Device_Error;
          end if;
@@ -1773,7 +2179,6 @@ package body Ada.Text_IO is
       --  we reset to text mode.
 
       if Needs_Binary_Write then
-
          if fflush (File.Stream) = -1 then
             raise Device_Error;
          end if;
@@ -1782,62 +2187,19 @@ package body Ada.Text_IO is
       end if;
    end Write;
 
-   --  Use "preallocated" strings to avoid calling "new" during the
-   --  elaboration of the run time. This is needed in the tasking case to
-   --  avoid calling Task_Lock too early. A filename is expected to end with a
-   --  null character in the runtime, here the null characters are added just
-   --  to have a correct filename length.
-
-   Err_Name : aliased String := "*stderr" & ASCII.Nul;
-   In_Name  : aliased String := "*stdin" & ASCII.Nul;
-   Out_Name : aliased String := "*stdout" & ASCII.Nul;
 begin
-   -------------------------------
-   -- Initialize Standard Files --
-   -------------------------------
+   --  Initialize Standard Files
 
-   --  Note: the names in these files are bogus, and probably it would be
-   --  better for these files to have no names, but the ACVC test insist!
-   --  We use names that are bound to fail in open etc.
+   for J in WC_Encoding_Method loop
+      if WC_Encoding = WC_Encoding_Letters (J) then
+         Default_WCEM := J;
+      end if;
+   end loop;
 
-   Standard_Err.Stream            := stderr;
-   Standard_Err.Name              := Err_Name'Access;
-   Standard_Err.Form              := Null_Str'Unrestricted_Access;
-   Standard_Err.Mode              := FCB.Out_File;
-   Standard_Err.Is_Regular_File   := is_regular_file (fileno (stderr)) /= 0;
-   Standard_Err.Is_Temporary_File := False;
-   Standard_Err.Is_System_File    := True;
-   Standard_Err.Is_Text_File      := True;
-   Standard_Err.Access_Method     := 'T';
-   Standard_Err.Self              := Standard_Err;
-
-   Standard_In.Stream             := stdin;
-   Standard_In.Name               := In_Name'Access;
-   Standard_In.Form               := Null_Str'Unrestricted_Access;
-   Standard_In.Mode               := FCB.In_File;
-   Standard_In.Is_Regular_File    := is_regular_file (fileno (stdin)) /= 0;
-   Standard_In.Is_Temporary_File  := False;
-   Standard_In.Is_System_File     := True;
-   Standard_In.Is_Text_File       := True;
-   Standard_In.Access_Method      := 'T';
-   Standard_In.Self               := Standard_In;
-
-   Standard_Out.Stream            := stdout;
-   Standard_Out.Name              := Out_Name'Access;
-   Standard_Out.Form              := Null_Str'Unrestricted_Access;
-   Standard_Out.Mode              := FCB.Out_File;
-   Standard_Out.Is_Regular_File   := is_regular_file (fileno (stdout)) /= 0;
-   Standard_Out.Is_Temporary_File := False;
-   Standard_Out.Is_System_File    := True;
-   Standard_Out.Is_Text_File      := True;
-   Standard_Out.Access_Method     := 'T';
-   Standard_Out.Self              := Standard_Out;
+   Initialize_Standard_Files;
 
    FIO.Chain_File (AP (Standard_In));
    FIO.Chain_File (AP (Standard_Out));
    FIO.Chain_File (AP (Standard_Err));
 
-   FIO.Make_Unbuffered (AP (Standard_Out));
-   FIO.Make_Unbuffered (AP (Standard_Err));
-
 end Ada.Text_IO;