OSDN Git Service

2010-12-06 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-textio.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                          A D A . T E X T _ I O                           --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2010, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 with Ada.Streams;             use Ada.Streams;
33 with Interfaces.C_Streams;    use Interfaces.C_Streams;
34
35 with System.File_IO;
36 with System.CRTL;
37 with System.WCh_Cnv;          use System.WCh_Cnv;
38 with System.WCh_Con;          use System.WCh_Con;
39
40 with Ada.Unchecked_Conversion;
41 with Ada.Unchecked_Deallocation;
42
43 pragma Elaborate_All (System.File_IO);
44 --  Needed because of calls to Chain_File in package body elaboration
45
46 package body Ada.Text_IO is
47
48    package FIO renames System.File_IO;
49
50    subtype AP is FCB.AFCB_Ptr;
51
52    function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
53    function To_TIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
54    use type FCB.File_Mode;
55
56    use type System.CRTL.size_t;
57
58    WC_Encoding : Character;
59    pragma Import (C, WC_Encoding, "__gl_wc_encoding");
60    --  Default wide character encoding
61
62    Err_Name : aliased String := "*stderr" & ASCII.NUL;
63    In_Name  : aliased String := "*stdin" & ASCII.NUL;
64    Out_Name : aliased String := "*stdout" & ASCII.NUL;
65    --  Names of standard files
66    --
67    --  Use "preallocated" strings to avoid calling "new" during the elaboration
68    --  of the run time. This is needed in the tasking case to avoid calling
69    --  Task_Lock too early. A filename is expected to end with a null character
70    --  in the runtime, here the null characters are added just to have a
71    --  correct filename length.
72    --
73    --  Note: the names for these files are bogus, and probably it would be
74    --  better for these files to have no names, but the ACVC tests insist!
75    --  We use names that are bound to fail in open etc.
76
77    Null_Str : aliased constant String := "";
78    --  Used as form string for standard files
79
80    -----------------------
81    -- Local Subprograms --
82    -----------------------
83
84    function Get_Upper_Half_Char
85      (C    : Character;
86       File : File_Type) return Character;
87    --  This function is shared by Get and Get_Immediate to extract an encoded
88    --  upper half character value from the given File. The first byte has
89    --  already been read and is passed in C. The character value is returned as
90    --  the result, and the file pointer is bumped past the character.
91    --  Constraint_Error is raised if the encoded value is outside the bounds of
92    --  type Character.
93
94    function Get_Upper_Half_Char_Immed
95      (C    : Character;
96       File : File_Type) return Character;
97    --  This routine is identical to Get_Upper_Half_Char, except that the reads
98    --  are done in Get_Immediate mode (i.e. without waiting for a line return).
99
100    function Getc (File : File_Type) return int;
101    --  Gets next character from file, which has already been checked for being
102    --  in read status, and returns the character read if no error occurs. The
103    --  result is EOF if the end of file was read.
104
105    function Getc_Immed (File : File_Type) return int;
106    --  This routine is identical to Getc, except that the read is done in
107    --  Get_Immediate mode (i.e. without waiting for a line return).
108
109    function Has_Upper_Half_Character (Item : String) return Boolean;
110    --  Returns True if any of the characters is in the range 16#80#-16#FF#
111
112    function Nextc (File : File_Type) return int;
113    --  Returns next character from file without skipping past it (i.e. it is a
114    --  combination of Getc followed by an Ungetc).
115
116    procedure Put_Encoded (File : File_Type; Char : Character);
117    --  Called to output a character Char to the given File, when the encoding
118    --  method for the file is other than brackets, and Char is upper half.
119
120    procedure Putc (ch : int; File : File_Type);
121    --  Outputs the given character to the file, which has already been checked
122    --  for being in output status. Device_Error is raised if the character
123    --  cannot be written.
124
125    procedure Set_WCEM (File : in out File_Type);
126    --  Called by Open and Create to set the wide character encoding method for
127    --  the file, processing a WCEM form parameter if one is present. File is
128    --  IN OUT because it may be closed in case of an error.
129
130    procedure Terminate_Line (File : File_Type);
131    --  If the file is in Write_File or Append_File mode, and the current line
132    --  is not terminated, then a line terminator is written using New_Line.
133    --  Note that there is no Terminate_Page routine, because the page mark at
134    --  the end of the file is implied if necessary.
135
136    procedure Ungetc (ch : int; File : File_Type);
137    --  Pushes back character into stream, using ungetc. The caller has checked
138    --  that the file is in read status. Device_Error is raised if the character
139    --  cannot be pushed back. An attempt to push back and end of file character
140    --  (EOF) is ignored.
141
142    -------------------
143    -- AFCB_Allocate --
144    -------------------
145
146    function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr is
147       pragma Unreferenced (Control_Block);
148    begin
149       return new Text_AFCB;
150    end AFCB_Allocate;
151
152    ----------------
153    -- AFCB_Close --
154    ----------------
155
156    procedure AFCB_Close (File : not null access Text_AFCB) is
157    begin
158       --  If the file being closed is one of the current files, then close
159       --  the corresponding current file. It is not clear that this action
160       --  is required (RM A.10.3(23)) but it seems reasonable, and besides
161       --  ACVC test CE3208A expects this behavior.
162
163       if File_Type (File) = Current_In then
164          Current_In := null;
165       elsif File_Type (File) = Current_Out then
166          Current_Out := null;
167       elsif File_Type (File) = Current_Err then
168          Current_Err := null;
169       end if;
170
171       Terminate_Line (File_Type (File));
172    end AFCB_Close;
173
174    ---------------
175    -- AFCB_Free --
176    ---------------
177
178    procedure AFCB_Free (File : not null access Text_AFCB) is
179       type FCB_Ptr is access all Text_AFCB;
180       FT : FCB_Ptr := FCB_Ptr (File);
181
182       procedure Free is new Ada.Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
183
184    begin
185       Free (FT);
186    end AFCB_Free;
187
188    -----------
189    -- Close --
190    -----------
191
192    procedure Close (File : in out File_Type) is
193    begin
194       FIO.Close (AP (File)'Unrestricted_Access);
195    end Close;
196
197    ---------
198    -- Col --
199    ---------
200
201    --  Note: we assume that it is impossible in practice for the column
202    --  to exceed the value of Count'Last, i.e. no check is required for
203    --  overflow raising layout error.
204
205    function Col (File : File_Type) return Positive_Count is
206    begin
207       FIO.Check_File_Open (AP (File));
208       return File.Col;
209    end Col;
210
211    function Col return Positive_Count is
212    begin
213       return Col (Current_Out);
214    end Col;
215
216    ------------
217    -- Create --
218    ------------
219
220    procedure Create
221      (File : in out File_Type;
222       Mode : File_Mode := Out_File;
223       Name : String := "";
224       Form : String := "")
225    is
226       Dummy_File_Control_Block : Text_AFCB;
227       pragma Warnings (Off, Dummy_File_Control_Block);
228       --  Yes, we know this is never assigned a value, only the tag
229       --  is used for dispatching purposes, so that's expected.
230
231    begin
232       FIO.Open (File_Ptr  => AP (File),
233                 Dummy_FCB => Dummy_File_Control_Block,
234                 Mode      => To_FCB (Mode),
235                 Name      => Name,
236                 Form      => Form,
237                 Amethod   => 'T',
238                 Creat     => True,
239                 Text      => True);
240
241       File.Self := File;
242       Set_WCEM (File);
243    end Create;
244
245    -------------------
246    -- Current_Error --
247    -------------------
248
249    function Current_Error return File_Type is
250    begin
251       return Current_Err;
252    end Current_Error;
253
254    function Current_Error return File_Access is
255    begin
256       return Current_Err.Self'Access;
257    end Current_Error;
258
259    -------------------
260    -- Current_Input --
261    -------------------
262
263    function Current_Input return File_Type is
264    begin
265       return Current_In;
266    end Current_Input;
267
268    function Current_Input return File_Access is
269    begin
270       return Current_In.Self'Access;
271    end Current_Input;
272
273    --------------------
274    -- Current_Output --
275    --------------------
276
277    function Current_Output return File_Type is
278    begin
279       return Current_Out;
280    end Current_Output;
281
282    function Current_Output return File_Access is
283    begin
284       return Current_Out.Self'Access;
285    end Current_Output;
286
287    ------------
288    -- Delete --
289    ------------
290
291    procedure Delete (File : in out File_Type) is
292    begin
293       FIO.Delete (AP (File)'Unrestricted_Access);
294    end Delete;
295
296    -----------------
297    -- End_Of_File --
298    -----------------
299
300    function End_Of_File (File : File_Type) return Boolean is
301       ch : int;
302
303    begin
304       FIO.Check_Read_Status (AP (File));
305
306       if File.Before_Upper_Half_Character then
307          return False;
308
309       elsif File.Before_LM then
310          if File.Before_LM_PM then
311             return Nextc (File) = EOF;
312          end if;
313
314       else
315          ch := Getc (File);
316
317          if ch = EOF then
318             return True;
319
320          elsif ch /= LM then
321             Ungetc (ch, File);
322             return False;
323
324          else -- ch = LM
325             File.Before_LM := True;
326          end if;
327       end if;
328
329       --  Here we are just past the line mark with Before_LM set so that we
330       --  do not have to try to back up past the LM, thus avoiding the need
331       --  to back up more than one character.
332
333       ch := Getc (File);
334
335       if ch = EOF then
336          return True;
337
338       elsif ch = PM and then File.Is_Regular_File then
339          File.Before_LM_PM := True;
340          return Nextc (File) = EOF;
341
342       --  Here if neither EOF nor PM followed end of line
343
344       else
345          Ungetc (ch, File);
346          return False;
347       end if;
348
349    end End_Of_File;
350
351    function End_Of_File return Boolean is
352    begin
353       return End_Of_File (Current_In);
354    end End_Of_File;
355
356    -----------------
357    -- End_Of_Line --
358    -----------------
359
360    function End_Of_Line (File : File_Type) return Boolean is
361       ch : int;
362
363    begin
364       FIO.Check_Read_Status (AP (File));
365
366       if File.Before_Upper_Half_Character then
367          return False;
368
369       elsif File.Before_LM then
370          return True;
371
372       else
373          ch := Getc (File);
374
375          if ch = EOF then
376             return True;
377
378          else
379             Ungetc (ch, File);
380             return (ch = LM);
381          end if;
382       end if;
383    end End_Of_Line;
384
385    function End_Of_Line return Boolean is
386    begin
387       return End_Of_Line (Current_In);
388    end End_Of_Line;
389
390    -----------------
391    -- End_Of_Page --
392    -----------------
393
394    function End_Of_Page (File : File_Type) return Boolean is
395       ch  : int;
396
397    begin
398       FIO.Check_Read_Status (AP (File));
399
400       if not File.Is_Regular_File then
401          return False;
402
403       elsif File.Before_Upper_Half_Character then
404          return False;
405
406       elsif File.Before_LM then
407          if File.Before_LM_PM then
408             return True;
409          end if;
410
411       else
412          ch := Getc (File);
413
414          if ch = EOF then
415             return True;
416
417          elsif ch /= LM then
418             Ungetc (ch, File);
419             return False;
420
421          else -- ch = LM
422             File.Before_LM := True;
423          end if;
424       end if;
425
426       --  Here we are just past the line mark with Before_LM set so that we
427       --  do not have to try to back up past the LM, thus avoiding the need
428       --  to back up more than one character.
429
430       ch := Nextc (File);
431
432       return ch = PM or else ch = EOF;
433    end End_Of_Page;
434
435    function End_Of_Page return Boolean is
436    begin
437       return End_Of_Page (Current_In);
438    end End_Of_Page;
439
440    --------------
441    -- EOF_Char --
442    --------------
443
444    function EOF_Char return Integer is
445    begin
446       return EOF;
447    end EOF_Char;
448
449    -----------
450    -- Flush --
451    -----------
452
453    procedure Flush (File : File_Type) is
454    begin
455       FIO.Flush (AP (File));
456    end Flush;
457
458    procedure Flush is
459    begin
460       Flush (Current_Out);
461    end Flush;
462
463    ----------
464    -- Form --
465    ----------
466
467    function Form (File : File_Type) return String is
468    begin
469       return FIO.Form (AP (File));
470    end Form;
471
472    ---------
473    -- Get --
474    ---------
475
476    procedure Get
477      (File : File_Type;
478       Item : out Character)
479    is
480       ch : int;
481
482    begin
483       FIO.Check_Read_Status (AP (File));
484
485       if File.Before_Upper_Half_Character then
486          File.Before_Upper_Half_Character := False;
487          Item := File.Saved_Upper_Half_Character;
488
489       elsif File.Before_LM then
490          File.Before_LM := False;
491          File.Col := 1;
492
493          if File.Before_LM_PM then
494             File.Line := 1;
495             File.Page := File.Page + 1;
496             File.Before_LM_PM := False;
497          else
498             File.Line := File.Line + 1;
499          end if;
500       end if;
501
502       loop
503          ch := Getc (File);
504
505          if ch = EOF then
506             raise End_Error;
507
508          elsif ch = LM then
509             File.Line := File.Line + 1;
510             File.Col := 1;
511
512          elsif ch = PM and then File.Is_Regular_File then
513             File.Page := File.Page + 1;
514             File.Line := 1;
515
516          else
517             Item := Character'Val (ch);
518             File.Col := File.Col + 1;
519             return;
520          end if;
521       end loop;
522    end Get;
523
524    procedure Get (Item : out Character) is
525    begin
526       Get (Current_In, Item);
527    end Get;
528
529    procedure Get
530      (File : File_Type;
531       Item : out String)
532    is
533       ch : int;
534       J  : Natural;
535
536    begin
537       FIO.Check_Read_Status (AP (File));
538
539       if File.Before_LM then
540          File.Before_LM := False;
541          File.Before_LM_PM := False;
542          File.Col := 1;
543
544          if File.Before_LM_PM then
545             File.Line := 1;
546             File.Page := File.Page + 1;
547             File.Before_LM_PM := False;
548
549          else
550             File.Line := File.Line + 1;
551          end if;
552       end if;
553
554       J := Item'First;
555       while J <= Item'Last loop
556          ch := Getc (File);
557
558          if ch = EOF then
559             raise End_Error;
560
561          elsif ch = LM then
562             File.Line := File.Line + 1;
563             File.Col := 1;
564
565          elsif ch = PM and then File.Is_Regular_File then
566             File.Page := File.Page + 1;
567             File.Line := 1;
568
569          else
570             Item (J) := Character'Val (ch);
571             J := J + 1;
572             File.Col := File.Col + 1;
573          end if;
574       end loop;
575    end Get;
576
577    procedure Get (Item : out String) is
578    begin
579       Get (Current_In, Item);
580    end Get;
581
582    -------------------
583    -- Get_Immediate --
584    -------------------
585
586    procedure Get_Immediate
587      (File : File_Type;
588       Item : out Character)
589    is
590       ch          : int;
591
592    begin
593       FIO.Check_Read_Status (AP (File));
594
595       if File.Before_Upper_Half_Character then
596          File.Before_Upper_Half_Character := False;
597          Item := File.Saved_Upper_Half_Character;
598
599       elsif File.Before_LM then
600          File.Before_LM := False;
601          File.Before_LM_PM := False;
602          Item := Character'Val (LM);
603
604       else
605          ch := Getc_Immed (File);
606
607          if ch = EOF then
608             raise End_Error;
609          else
610             Item :=
611               (if not Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
612                then Character'Val (ch)
613                else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
614          end if;
615       end if;
616    end Get_Immediate;
617
618    procedure Get_Immediate
619      (Item : out Character)
620    is
621    begin
622       Get_Immediate (Current_In, Item);
623    end Get_Immediate;
624
625    procedure Get_Immediate
626      (File      : File_Type;
627       Item      : out Character;
628       Available : out Boolean)
629    is
630       ch          : int;
631       end_of_file : int;
632       avail       : int;
633
634       procedure getc_immediate_nowait
635         (stream      : FILEs;
636          ch          : out int;
637          end_of_file : out int;
638          avail       : out int);
639       pragma Import (C, getc_immediate_nowait, "getc_immediate_nowait");
640
641    begin
642       FIO.Check_Read_Status (AP (File));
643       Available := True;
644
645       if File.Before_Upper_Half_Character then
646          File.Before_Upper_Half_Character := False;
647          Item := File.Saved_Upper_Half_Character;
648
649       elsif File.Before_LM then
650          File.Before_LM := False;
651          File.Before_LM_PM := False;
652          Item := Character'Val (LM);
653
654       else
655          getc_immediate_nowait (File.Stream, ch, end_of_file, avail);
656
657          if ferror (File.Stream) /= 0 then
658             raise Device_Error;
659
660          elsif end_of_file /= 0 then
661             raise End_Error;
662
663          elsif avail = 0 then
664             Available := False;
665             Item := ASCII.NUL;
666
667          else
668             Available := True;
669
670             Item :=
671               (if Is_Start_Of_Encoding (Character'Val (ch), File.WC_Method)
672                then Character'Val (ch)
673                else Get_Upper_Half_Char_Immed (Character'Val (ch), File));
674          end if;
675       end if;
676
677    end Get_Immediate;
678
679    procedure Get_Immediate
680      (Item      : out Character;
681       Available : out Boolean)
682    is
683    begin
684       Get_Immediate (Current_In, Item, Available);
685    end Get_Immediate;
686
687    --------------
688    -- Get_Line --
689    --------------
690
691    procedure Get_Line
692      (File : File_Type;
693       Item : out String;
694       Last : out Natural) is separate;
695    --  The implementation of Ada.Text_IO.Get_Line is split into a subunit so
696    --  that different implementations can be used on different systems. In
697    --  particular the standard implementation uses low level stuff that is
698    --  not appropriate for the JVM and .NET implementations.
699
700    procedure Get_Line
701      (Item : out String;
702       Last : out Natural)
703    is
704    begin
705       Get_Line (Current_In, Item, Last);
706    end Get_Line;
707
708    function Get_Line (File : File_Type) return String is
709       Buffer : String (1 .. 500);
710       Last   : Natural;
711
712       function Get_Rest (S : String) return String;
713       --  This is a recursive function that reads the rest of the line and
714       --  returns it. S is the part read so far.
715
716       --------------
717       -- Get_Rest --
718       --------------
719
720       function Get_Rest (S : String) return String is
721
722          --  Each time we allocate a buffer the same size as what we have
723          --  read so far. This limits us to a logarithmic number of calls
724          --  to Get_Rest and also ensures only a linear use of stack space.
725
726          Buffer : String (1 .. S'Length);
727          Last   : Natural;
728
729       begin
730          Get_Line (File, Buffer, Last);
731
732          declare
733             R : constant String := S & Buffer (1 .. Last);
734          begin
735             if Last < Buffer'Last then
736                return R;
737             else
738                return Get_Rest (R);
739             end if;
740          end;
741       end Get_Rest;
742
743    --  Start of processing for Get_Line
744
745    begin
746       Get_Line (File, Buffer, Last);
747
748       if Last < Buffer'Last then
749          return Buffer (1 .. Last);
750       else
751          return Get_Rest (Buffer (1 .. Last));
752       end if;
753    end Get_Line;
754
755    function Get_Line return String is
756    begin
757       return Get_Line (Current_In);
758    end Get_Line;
759
760    -------------------------
761    -- Get_Upper_Half_Char --
762    -------------------------
763
764    function Get_Upper_Half_Char
765      (C    : Character;
766       File : File_Type) return Character
767    is
768       Result : Wide_Character;
769
770       function In_Char return Character;
771       --  Function used to obtain additional characters it the wide character
772       --  sequence is more than one character long.
773
774       function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
775
776       -------------
777       -- In_Char --
778       -------------
779
780       function In_Char return Character is
781          ch : constant Integer := Getc (File);
782       begin
783          if ch = EOF then
784             raise End_Error;
785          else
786             return Character'Val (ch);
787          end if;
788       end In_Char;
789
790    --  Start of processing for Get_Upper_Half_Char
791
792    begin
793       Result := WC_In (C, File.WC_Method);
794
795       if Wide_Character'Pos (Result) > 16#FF# then
796          raise Constraint_Error with
797            "invalid wide character in Text_'I'O input";
798       else
799          return Character'Val (Wide_Character'Pos (Result));
800       end if;
801    end Get_Upper_Half_Char;
802
803    -------------------------------
804    -- Get_Upper_Half_Char_Immed --
805    -------------------------------
806
807    function Get_Upper_Half_Char_Immed
808      (C    : Character;
809       File : File_Type) return Character
810    is
811       Result : Wide_Character;
812
813       function In_Char return Character;
814       --  Function used to obtain additional characters it the wide character
815       --  sequence is more than one character long.
816
817       function WC_In is new Char_Sequence_To_Wide_Char (In_Char);
818
819       -------------
820       -- In_Char --
821       -------------
822
823       function In_Char return Character is
824          ch : constant Integer := Getc_Immed (File);
825       begin
826          if ch = EOF then
827             raise End_Error;
828          else
829             return Character'Val (ch);
830          end if;
831       end In_Char;
832
833    --  Start of processing for Get_Upper_Half_Char_Immed
834
835    begin
836       Result := WC_In (C, File.WC_Method);
837
838       if Wide_Character'Pos (Result) > 16#FF# then
839          raise Constraint_Error with
840            "invalid wide character in Text_'I'O input";
841       else
842          return Character'Val (Wide_Character'Pos (Result));
843       end if;
844    end Get_Upper_Half_Char_Immed;
845
846    ----------
847    -- Getc --
848    ----------
849
850    function Getc (File : File_Type) return int is
851       ch : int;
852
853    begin
854       ch := fgetc (File.Stream);
855
856       if ch = EOF and then ferror (File.Stream) /= 0 then
857          raise Device_Error;
858       else
859          return ch;
860       end if;
861    end Getc;
862
863    ----------------
864    -- Getc_Immed --
865    ----------------
866
867    function Getc_Immed (File : File_Type) return int is
868       ch          : int;
869       end_of_file : int;
870
871       procedure getc_immediate
872         (stream : FILEs; ch : out int; end_of_file : out int);
873       pragma Import (C, getc_immediate, "getc_immediate");
874
875    begin
876       FIO.Check_Read_Status (AP (File));
877
878       if File.Before_LM then
879          File.Before_LM := False;
880          File.Before_LM_PM := False;
881          ch := LM;
882
883       else
884          getc_immediate (File.Stream, ch, end_of_file);
885
886          if ferror (File.Stream) /= 0 then
887             raise Device_Error;
888          elsif end_of_file /= 0 then
889             return EOF;
890          end if;
891       end if;
892
893       return ch;
894    end Getc_Immed;
895
896    ------------------------------
897    -- Has_Upper_Half_Character --
898    ------------------------------
899
900    function Has_Upper_Half_Character (Item : String) return Boolean is
901    begin
902       for J in Item'Range loop
903          if Character'Pos (Item (J)) >= 16#80# then
904             return True;
905          end if;
906       end loop;
907
908       return False;
909    end Has_Upper_Half_Character;
910
911    -------------------------------
912    -- Initialize_Standard_Files --
913    -------------------------------
914
915    procedure Initialize_Standard_Files is
916    begin
917       Standard_Err.Stream            := stderr;
918       Standard_Err.Name              := Err_Name'Access;
919       Standard_Err.Form              := Null_Str'Unrestricted_Access;
920       Standard_Err.Mode              := FCB.Out_File;
921       Standard_Err.Is_Regular_File   := is_regular_file (fileno (stderr)) /= 0;
922       Standard_Err.Is_Temporary_File := False;
923       Standard_Err.Is_System_File    := True;
924       Standard_Err.Is_Text_File      := True;
925       Standard_Err.Access_Method     := 'T';
926       Standard_Err.Self              := Standard_Err;
927       Standard_Err.WC_Method         := Default_WCEM;
928
929       Standard_In.Stream             := stdin;
930       Standard_In.Name               := In_Name'Access;
931       Standard_In.Form               := Null_Str'Unrestricted_Access;
932       Standard_In.Mode               := FCB.In_File;
933       Standard_In.Is_Regular_File    := is_regular_file (fileno (stdin)) /= 0;
934       Standard_In.Is_Temporary_File  := False;
935       Standard_In.Is_System_File     := True;
936       Standard_In.Is_Text_File       := True;
937       Standard_In.Access_Method      := 'T';
938       Standard_In.Self               := Standard_In;
939       Standard_In.WC_Method          := Default_WCEM;
940
941       Standard_Out.Stream            := stdout;
942       Standard_Out.Name              := Out_Name'Access;
943       Standard_Out.Form              := Null_Str'Unrestricted_Access;
944       Standard_Out.Mode              := FCB.Out_File;
945       Standard_Out.Is_Regular_File   := is_regular_file (fileno (stdout)) /= 0;
946       Standard_Out.Is_Temporary_File := False;
947       Standard_Out.Is_System_File    := True;
948       Standard_Out.Is_Text_File      := True;
949       Standard_Out.Access_Method     := 'T';
950       Standard_Out.Self              := Standard_Out;
951       Standard_Out.WC_Method         := Default_WCEM;
952
953       FIO.Make_Unbuffered (AP (Standard_Out));
954       FIO.Make_Unbuffered (AP (Standard_Err));
955    end Initialize_Standard_Files;
956
957    -------------
958    -- Is_Open --
959    -------------
960
961    function Is_Open (File : File_Type) return Boolean is
962    begin
963       return FIO.Is_Open (AP (File));
964    end Is_Open;
965
966    ----------
967    -- Line --
968    ----------
969
970    --  Note: we assume that it is impossible in practice for the line
971    --  to exceed the value of Count'Last, i.e. no check is required for
972    --  overflow raising layout error.
973
974    function Line (File : File_Type) return Positive_Count is
975    begin
976       FIO.Check_File_Open (AP (File));
977       return File.Line;
978    end Line;
979
980    function Line return Positive_Count is
981    begin
982       return Line (Current_Out);
983    end Line;
984
985    -----------------
986    -- Line_Length --
987    -----------------
988
989    function Line_Length (File : File_Type) return Count is
990    begin
991       FIO.Check_Write_Status (AP (File));
992       return File.Line_Length;
993    end Line_Length;
994
995    function Line_Length return Count is
996    begin
997       return Line_Length (Current_Out);
998    end Line_Length;
999
1000    ----------------
1001    -- Look_Ahead --
1002    ----------------
1003
1004    procedure Look_Ahead
1005      (File        : File_Type;
1006       Item        : out Character;
1007       End_Of_Line : out Boolean)
1008    is
1009       ch : int;
1010
1011    begin
1012       FIO.Check_Read_Status (AP (File));
1013
1014       --  If we are logically before a line mark, we can return immediately
1015
1016       if File.Before_LM then
1017          End_Of_Line := True;
1018          Item := ASCII.NUL;
1019
1020       --  If we are before an upper half character just return it (this can
1021       --  happen if there are two calls to Look_Ahead in a row).
1022
1023       elsif File.Before_Upper_Half_Character then
1024          End_Of_Line := False;
1025          Item := File.Saved_Upper_Half_Character;
1026
1027       --  Otherwise we must read a character from the input stream
1028
1029       else
1030          ch := Getc (File);
1031
1032          if ch = LM
1033            or else ch = EOF
1034            or else (ch = PM and then File.Is_Regular_File)
1035          then
1036             End_Of_Line := True;
1037             Ungetc (ch, File);
1038             Item := ASCII.NUL;
1039
1040          --  Case where character obtained does not represent the start of an
1041          --  encoded sequence so it stands for itself and we can unget it with
1042          --  no difficulty.
1043
1044          elsif not Is_Start_Of_Encoding
1045                      (Character'Val (ch), File.WC_Method)
1046          then
1047             End_Of_Line := False;
1048             Ungetc (ch, File);
1049             Item := Character'Val (ch);
1050
1051          --  For the start of an encoding, we read the character using the
1052          --  Get_Upper_Half_Char routine. It will occupy more than one byte
1053          --  so we can't put it back with ungetc. Instead we save it in the
1054          --  control block, setting a flag that everyone interested in reading
1055          --  characters must test before reading the stream.
1056
1057          else
1058             Item := Get_Upper_Half_Char (Character'Val (ch), File);
1059             End_Of_Line := False;
1060             File.Saved_Upper_Half_Character := Item;
1061             File.Before_Upper_Half_Character := True;
1062          end if;
1063       end if;
1064    end Look_Ahead;
1065
1066    procedure Look_Ahead
1067      (Item        : out Character;
1068       End_Of_Line : out Boolean)
1069    is
1070    begin
1071       Look_Ahead (Current_In, Item, End_Of_Line);
1072    end Look_Ahead;
1073
1074    ----------
1075    -- Mode --
1076    ----------
1077
1078    function Mode (File : File_Type) return File_Mode is
1079    begin
1080       return To_TIO (FIO.Mode (AP (File)));
1081    end Mode;
1082
1083    ----------
1084    -- Name --
1085    ----------
1086
1087    function Name (File : File_Type) return String is
1088    begin
1089       return FIO.Name (AP (File));
1090    end Name;
1091
1092    --------------
1093    -- New_Line --
1094    --------------
1095
1096    procedure New_Line
1097      (File    : File_Type;
1098       Spacing : Positive_Count := 1)
1099    is
1100    begin
1101       --  Raise Constraint_Error if out of range value. The reason for this
1102       --  explicit test is that we don't want junk values around, even if
1103       --  checks are off in the caller.
1104
1105       if not Spacing'Valid then
1106          raise Constraint_Error;
1107       end if;
1108
1109       FIO.Check_Write_Status (AP (File));
1110
1111       for K in 1 .. Spacing loop
1112          Putc (LM, File);
1113          File.Line := File.Line + 1;
1114
1115          if File.Page_Length /= 0
1116            and then File.Line > File.Page_Length
1117          then
1118             Putc (PM, File);
1119             File.Line := 1;
1120             File.Page := File.Page + 1;
1121          end if;
1122       end loop;
1123
1124       File.Col := 1;
1125    end New_Line;
1126
1127    procedure New_Line (Spacing : Positive_Count := 1) is
1128    begin
1129       New_Line (Current_Out, Spacing);
1130    end New_Line;
1131
1132    --------------
1133    -- New_Page --
1134    --------------
1135
1136    procedure New_Page (File : File_Type) is
1137    begin
1138       FIO.Check_Write_Status (AP (File));
1139
1140       if File.Col /= 1 or else File.Line = 1 then
1141          Putc (LM, File);
1142       end if;
1143
1144       Putc (PM, File);
1145       File.Page := File.Page + 1;
1146       File.Line := 1;
1147       File.Col := 1;
1148    end New_Page;
1149
1150    procedure New_Page is
1151    begin
1152       New_Page (Current_Out);
1153    end New_Page;
1154
1155    -----------
1156    -- Nextc --
1157    -----------
1158
1159    function Nextc (File : File_Type) return int is
1160       ch : int;
1161
1162    begin
1163       ch := fgetc (File.Stream);
1164
1165       if ch = EOF then
1166          if ferror (File.Stream) /= 0 then
1167             raise Device_Error;
1168          end if;
1169
1170       else
1171          if ungetc (ch, File.Stream) = EOF then
1172             raise Device_Error;
1173          end if;
1174       end if;
1175
1176       return ch;
1177    end Nextc;
1178
1179    ----------
1180    -- Open --
1181    ----------
1182
1183    procedure Open
1184      (File : in out File_Type;
1185       Mode : File_Mode;
1186       Name : String;
1187       Form : String := "")
1188    is
1189       Dummy_File_Control_Block : Text_AFCB;
1190       pragma Warnings (Off, Dummy_File_Control_Block);
1191       --  Yes, we know this is never assigned a value, only the tag
1192       --  is used for dispatching purposes, so that's expected.
1193
1194    begin
1195       FIO.Open (File_Ptr  => AP (File),
1196                 Dummy_FCB => Dummy_File_Control_Block,
1197                 Mode      => To_FCB (Mode),
1198                 Name      => Name,
1199                 Form      => Form,
1200                 Amethod   => 'T',
1201                 Creat     => False,
1202                 Text      => True);
1203
1204       File.Self := File;
1205       Set_WCEM (File);
1206    end Open;
1207
1208    ----------
1209    -- Page --
1210    ----------
1211
1212    --  Note: we assume that it is impossible in practice for the page
1213    --  to exceed the value of Count'Last, i.e. no check is required for
1214    --  overflow raising layout error.
1215
1216    function Page (File : File_Type) return Positive_Count is
1217    begin
1218       FIO.Check_File_Open (AP (File));
1219       return File.Page;
1220    end Page;
1221
1222    function Page return Positive_Count is
1223    begin
1224       return Page (Current_Out);
1225    end Page;
1226
1227    -----------------
1228    -- Page_Length --
1229    -----------------
1230
1231    function Page_Length (File : File_Type) return Count is
1232    begin
1233       FIO.Check_Write_Status (AP (File));
1234       return File.Page_Length;
1235    end Page_Length;
1236
1237    function Page_Length return Count is
1238    begin
1239       return Page_Length (Current_Out);
1240    end Page_Length;
1241
1242    ---------
1243    -- Put --
1244    ---------
1245
1246    procedure Put
1247      (File : File_Type;
1248       Item : Character)
1249    is
1250    begin
1251       FIO.Check_Write_Status (AP (File));
1252
1253       if File.Line_Length /= 0 and then File.Col > File.Line_Length then
1254          New_Line (File);
1255       end if;
1256
1257       --  If lower half character, or brackets encoding, output directly
1258
1259       if Character'Pos (Item) < 16#80#
1260         or else File.WC_Method = WCEM_Brackets
1261       then
1262          if fputc (Character'Pos (Item), File.Stream) = EOF then
1263             raise Device_Error;
1264          end if;
1265
1266       --  Case of upper half character with non-brackets encoding
1267
1268       else
1269          Put_Encoded (File, Item);
1270       end if;
1271
1272       File.Col := File.Col + 1;
1273    end Put;
1274
1275    procedure Put (Item : Character) is
1276    begin
1277       FIO.Check_Write_Status (AP (Current_Out));
1278
1279       if Current_Out.Line_Length /= 0
1280         and then Current_Out.Col > Current_Out.Line_Length
1281       then
1282          New_Line (Current_Out);
1283       end if;
1284
1285       --  If lower half character, or brackets encoding, output directly
1286
1287       if Character'Pos (Item) < 16#80#
1288         or else Default_WCEM = WCEM_Brackets
1289       then
1290          if fputc (Character'Pos (Item), Current_Out.Stream) = EOF then
1291             raise Device_Error;
1292          end if;
1293
1294       --  Case of upper half character with non-brackets encoding
1295
1296       else
1297          Put_Encoded (Current_Out, Item);
1298       end if;
1299
1300       Current_Out.Col := Current_Out.Col + 1;
1301    end Put;
1302
1303    ---------
1304    -- Put --
1305    ---------
1306
1307    procedure Put
1308      (File : File_Type;
1309       Item : String)
1310    is
1311    begin
1312       FIO.Check_Write_Status (AP (File));
1313
1314       --  Only have something to do if string is non-null
1315
1316       if Item'Length > 0 then
1317
1318          --  If we have bounded lines, or if the file encoding is other than
1319          --  Brackets and the string has at least one upper half character,
1320          --  then output the string character by character.
1321
1322          if File.Line_Length /= 0
1323            or else (File.WC_Method /= WCEM_Brackets
1324                       and then Has_Upper_Half_Character (Item))
1325          then
1326             for J in Item'Range loop
1327                Put (File, Item (J));
1328             end loop;
1329
1330          --  Otherwise we can output the entire string at once. Note that if
1331          --  there are LF or FF characters in the string, we do not bother to
1332          --  count them as line or page terminators.
1333
1334          else
1335             FIO.Write_Buf (AP (File), Item'Address, Item'Length);
1336             File.Col := File.Col + Item'Length;
1337          end if;
1338       end if;
1339    end Put;
1340
1341    procedure Put (Item : String) is
1342    begin
1343       Put (Current_Out, Item);
1344    end Put;
1345
1346    -----------------
1347    -- Put_Encoded --
1348    -----------------
1349
1350    procedure Put_Encoded (File : File_Type; Char : Character) is
1351       procedure Out_Char (C : Character);
1352       --  Procedure to output one character of an upper half encoded sequence
1353
1354       procedure WC_Out is new Wide_Char_To_Char_Sequence (Out_Char);
1355
1356       --------------
1357       -- Out_Char --
1358       --------------
1359
1360       procedure Out_Char (C : Character) is
1361       begin
1362          Putc (Character'Pos (C), File);
1363       end Out_Char;
1364
1365    --  Start of processing for Put_Encoded
1366
1367    begin
1368       WC_Out (Wide_Character'Val (Character'Pos (Char)), File.WC_Method);
1369    end Put_Encoded;
1370
1371    --------------
1372    -- Put_Line --
1373    --------------
1374
1375    procedure Put_Line
1376      (File : File_Type;
1377       Item : String)
1378    is
1379       Ilen   : Natural := Item'Length;
1380       Istart : Natural := Item'First;
1381
1382    begin
1383       FIO.Check_Write_Status (AP (File));
1384
1385       --  If we have bounded lines, or if the file encoding is other than
1386       --  Brackets and the string has at least one upper half character, then
1387       --  output the string character by character.
1388
1389       if File.Line_Length /= 0
1390         or else (File.WC_Method /= WCEM_Brackets
1391                    and then Has_Upper_Half_Character (Item))
1392       then
1393          for J in Item'Range loop
1394             Put (File, Item (J));
1395          end loop;
1396
1397          New_Line (File);
1398          return;
1399       end if;
1400
1401       --  Normal case where we do not need to output character by character
1402
1403       --  We setup a single string that has the necessary terminators and
1404       --  then write it with a single call. The reason for doing this is
1405       --  that it gives better behavior for the use of Put_Line in multi-
1406       --  tasking programs, since often the OS will treat the entire put
1407       --  operation as an atomic operation.
1408
1409       --  We only do this if the message is 512 characters or less in length,
1410       --  since otherwise Put_Line would use an unbounded amount of stack
1411       --  space and could cause undetected stack overflow. If we have a
1412       --  longer string, then output the first part separately to avoid this.
1413
1414       if Ilen > 512 then
1415          FIO.Write_Buf (AP (File), Item'Address, size_t (Ilen - 512));
1416          Istart := Istart + Ilen - 512;
1417          Ilen   := 512;
1418       end if;
1419
1420       --  Now prepare the string with its terminator
1421
1422       declare
1423          Buffer : String (1 .. Ilen + 2);
1424          Plen   : size_t;
1425
1426       begin
1427          Buffer (1 .. Ilen) := Item (Istart .. Item'Last);
1428          Buffer (Ilen + 1) := Character'Val (LM);
1429
1430          if File.Page_Length /= 0
1431            and then File.Line > File.Page_Length
1432          then
1433             Buffer (Ilen + 2) := Character'Val (PM);
1434             Plen := size_t (Ilen) + 2;
1435             File.Line := 1;
1436             File.Page := File.Page + 1;
1437
1438          else
1439             Plen := size_t (Ilen) + 1;
1440             File.Line := File.Line + 1;
1441          end if;
1442
1443          FIO.Write_Buf (AP (File), Buffer'Address, Plen);
1444
1445          File.Col := 1;
1446       end;
1447    end Put_Line;
1448
1449    procedure Put_Line (Item : String) is
1450    begin
1451       Put_Line (Current_Out, Item);
1452    end Put_Line;
1453
1454    ----------
1455    -- Putc --
1456    ----------
1457
1458    procedure Putc (ch : int; File : File_Type) is
1459    begin
1460       if fputc (ch, File.Stream) = EOF then
1461          raise Device_Error;
1462       end if;
1463    end Putc;
1464
1465    ----------
1466    -- Read --
1467    ----------
1468
1469    --  This is the primitive Stream Read routine, used when a Text_IO file
1470    --  is treated directly as a stream using Text_IO.Streams.Stream.
1471
1472    procedure Read
1473      (File : in out Text_AFCB;
1474       Item : out Stream_Element_Array;
1475       Last : out Stream_Element_Offset)
1476    is
1477       Discard_ch : int;
1478       pragma Warnings (Off, Discard_ch);
1479
1480    begin
1481       --  Need to deal with Before_Upper_Half_Character ???
1482
1483       if File.Mode /= FCB.In_File then
1484          raise Mode_Error;
1485       end if;
1486
1487       --  Deal with case where our logical and physical position do not match
1488       --  because of being after an LM or LM-PM sequence when in fact we are
1489       --  logically positioned before it.
1490
1491       if File.Before_LM then
1492
1493          --  If we are before a PM, then it is possible for a stream read
1494          --  to leave us after the LM and before the PM, which is a bit
1495          --  odd. The easiest way to deal with this is to unget the PM,
1496          --  so we are indeed positioned between the characters. This way
1497          --  further stream read operations will work correctly, and the
1498          --  effect on text processing is a little weird, but what can
1499          --  be expected if stream and text input are mixed this way?
1500
1501          if File.Before_LM_PM then
1502             Discard_ch := ungetc (PM, File.Stream);
1503             File.Before_LM_PM := False;
1504          end if;
1505
1506          File.Before_LM := False;
1507
1508          Item (Item'First) := Stream_Element (Character'Pos (ASCII.LF));
1509
1510          if Item'Length = 1 then
1511             Last := Item'Last;
1512
1513          else
1514             Last :=
1515               Item'First +
1516                 Stream_Element_Offset
1517                   (fread (buffer => Item'Address,
1518                           index  => size_t (Item'First + 1),
1519                           size   => 1,
1520                           count  => Item'Length - 1,
1521                           stream => File.Stream));
1522          end if;
1523
1524          return;
1525       end if;
1526
1527       --  Now we do the read. Since this is a text file, it is normally in
1528       --  text mode, but stream data must be read in binary mode, so we
1529       --  temporarily set binary mode for the read, resetting it after.
1530       --  These calls have no effect in a system (like Unix) where there is
1531       --  no distinction between text and binary files.
1532
1533       set_binary_mode (fileno (File.Stream));
1534
1535       Last :=
1536         Item'First +
1537           Stream_Element_Offset
1538             (fread (Item'Address, 1, Item'Length, File.Stream)) - 1;
1539
1540       if Last < Item'Last then
1541          if ferror (File.Stream) /= 0 then
1542             raise Device_Error;
1543          end if;
1544       end if;
1545
1546       set_text_mode (fileno (File.Stream));
1547    end Read;
1548
1549    -----------
1550    -- Reset --
1551    -----------
1552
1553    procedure Reset
1554      (File : in out File_Type;
1555       Mode : File_Mode)
1556    is
1557    begin
1558       --  Don't allow change of mode for current file (RM A.10.2(5))
1559
1560       if (File = Current_In  or else
1561           File = Current_Out or else
1562           File = Current_Error)
1563         and then To_FCB (Mode) /= File.Mode
1564       then
1565          raise Mode_Error;
1566       end if;
1567
1568       Terminate_Line (File);
1569       FIO.Reset (AP (File)'Unrestricted_Access, To_FCB (Mode));
1570       File.Page := 1;
1571       File.Line := 1;
1572       File.Col  := 1;
1573       File.Line_Length := 0;
1574       File.Page_Length := 0;
1575       File.Before_LM := False;
1576       File.Before_LM_PM := False;
1577    end Reset;
1578
1579    procedure Reset (File : in out File_Type) is
1580    begin
1581       Terminate_Line (File);
1582       FIO.Reset (AP (File)'Unrestricted_Access);
1583       File.Page := 1;
1584       File.Line := 1;
1585       File.Col  := 1;
1586       File.Line_Length := 0;
1587       File.Page_Length := 0;
1588       File.Before_LM := False;
1589       File.Before_LM_PM := False;
1590    end Reset;
1591
1592    -------------
1593    -- Set_Col --
1594    -------------
1595
1596    procedure Set_Col
1597      (File : File_Type;
1598       To   : Positive_Count)
1599    is
1600       ch : int;
1601
1602    begin
1603       --  Raise Constraint_Error if out of range value. The reason for this
1604       --  explicit test is that we don't want junk values around, even if
1605       --  checks are off in the caller.
1606
1607       if not To'Valid then
1608          raise Constraint_Error;
1609       end if;
1610
1611       FIO.Check_File_Open (AP (File));
1612
1613       --  Output case
1614
1615       if Mode (File) >= Out_File then
1616
1617          --  Error if we attempt to set Col to a value greater than the
1618          --  maximum permissible line length.
1619
1620          if File.Line_Length /= 0 and then To > File.Line_Length then
1621             raise Layout_Error;
1622          end if;
1623
1624          --  If we are behind current position, then go to start of new line
1625
1626          if To < File.Col then
1627             New_Line (File);
1628          end if;
1629
1630          --  Loop to output blanks till we are at the required column
1631
1632          while File.Col < To loop
1633             Put (File, ' ');
1634          end loop;
1635
1636       --  Input case
1637
1638       else
1639          --  If we are logically before a LM, but physically after it, the
1640          --  file position still reflects the position before the LM, so eat
1641          --  it now and adjust the file position appropriately.
1642
1643          if File.Before_LM then
1644             File.Before_LM := False;
1645             File.Before_LM_PM := False;
1646             File.Line := File.Line + 1;
1647             File.Col := 1;
1648          end if;
1649
1650          --  Loop reading characters till we get one at the required Col value
1651
1652          loop
1653             --  Read next character. The reason we have to read ahead is to
1654             --  skip formatting characters, the effect of Set_Col is to set
1655             --  us to a real character with the right Col value, and format
1656             --  characters don't count.
1657
1658             ch := Getc (File);
1659
1660             --  Error if we hit an end of file
1661
1662             if ch = EOF then
1663                raise End_Error;
1664
1665             --  If line mark, eat it and adjust file position
1666
1667             elsif ch = LM then
1668                File.Line := File.Line + 1;
1669                File.Col := 1;
1670
1671             --  If recognized page mark, eat it, and adjust file position
1672
1673             elsif ch = PM and then File.Is_Regular_File then
1674                File.Page := File.Page + 1;
1675                File.Line := 1;
1676                File.Col := 1;
1677
1678             --  Otherwise this is the character we are looking for, so put it
1679             --  back in the input stream (we have not adjusted the file
1680             --  position yet, so everything is set right after this ungetc).
1681
1682             elsif To = File.Col then
1683                Ungetc (ch, File);
1684                return;
1685
1686             --  Keep skipping characters if we are not there yet, updating the
1687             --  file position past the skipped character.
1688
1689             else
1690                File.Col := File.Col + 1;
1691             end if;
1692          end loop;
1693       end if;
1694    end Set_Col;
1695
1696    procedure Set_Col (To : Positive_Count) is
1697    begin
1698       Set_Col (Current_Out, To);
1699    end Set_Col;
1700
1701    ---------------
1702    -- Set_Error --
1703    ---------------
1704
1705    procedure Set_Error (File : File_Type) is
1706    begin
1707       FIO.Check_Write_Status (AP (File));
1708       Current_Err := File;
1709    end Set_Error;
1710
1711    ---------------
1712    -- Set_Input --
1713    ---------------
1714
1715    procedure Set_Input (File : File_Type) is
1716    begin
1717       FIO.Check_Read_Status (AP (File));
1718       Current_In := File;
1719    end Set_Input;
1720
1721    --------------
1722    -- Set_Line --
1723    --------------
1724
1725    procedure Set_Line
1726      (File : File_Type;
1727       To   : Positive_Count)
1728    is
1729    begin
1730       --  Raise Constraint_Error if out of range value. The reason for this
1731       --  explicit test is that we don't want junk values around, even if
1732       --  checks are off in the caller.
1733
1734       if not To'Valid then
1735          raise Constraint_Error;
1736       end if;
1737
1738       FIO.Check_File_Open (AP (File));
1739
1740       if To = File.Line then
1741          return;
1742       end if;
1743
1744       if Mode (File) >= Out_File then
1745          if File.Page_Length /= 0 and then To > File.Page_Length then
1746             raise Layout_Error;
1747          end if;
1748
1749          if To < File.Line then
1750             New_Page (File);
1751          end if;
1752
1753          while File.Line < To loop
1754             New_Line (File);
1755          end loop;
1756
1757       else
1758          while To /= File.Line loop
1759             Skip_Line (File);
1760          end loop;
1761       end if;
1762    end Set_Line;
1763
1764    procedure Set_Line (To : Positive_Count) is
1765    begin
1766       Set_Line (Current_Out, To);
1767    end Set_Line;
1768
1769    ---------------------
1770    -- Set_Line_Length --
1771    ---------------------
1772
1773    procedure Set_Line_Length (File : File_Type; To : Count) is
1774    begin
1775       --  Raise Constraint_Error if out of range value. The reason for this
1776       --  explicit test is that we don't want junk values around, even if
1777       --  checks are off in the caller.
1778
1779       if not To'Valid then
1780          raise Constraint_Error;
1781       end if;
1782
1783       FIO.Check_Write_Status (AP (File));
1784       File.Line_Length := To;
1785    end Set_Line_Length;
1786
1787    procedure Set_Line_Length (To : Count) is
1788    begin
1789       Set_Line_Length (Current_Out, To);
1790    end Set_Line_Length;
1791
1792    ----------------
1793    -- Set_Output --
1794    ----------------
1795
1796    procedure Set_Output (File : File_Type) is
1797    begin
1798       FIO.Check_Write_Status (AP (File));
1799       Current_Out := File;
1800    end Set_Output;
1801
1802    ---------------------
1803    -- Set_Page_Length --
1804    ---------------------
1805
1806    procedure Set_Page_Length (File : File_Type; To : Count) is
1807    begin
1808       --  Raise Constraint_Error if out of range value. The reason for this
1809       --  explicit test is that we don't want junk values around, even if
1810       --  checks are off in the caller.
1811
1812       if not To'Valid then
1813          raise Constraint_Error;
1814       end if;
1815
1816       FIO.Check_Write_Status (AP (File));
1817       File.Page_Length := To;
1818    end Set_Page_Length;
1819
1820    procedure Set_Page_Length (To : Count) is
1821    begin
1822       Set_Page_Length (Current_Out, To);
1823    end Set_Page_Length;
1824
1825    --------------
1826    -- Set_WCEM --
1827    --------------
1828
1829    procedure Set_WCEM (File : in out File_Type) is
1830       Start : Natural;
1831       Stop  : Natural;
1832
1833    begin
1834       File.WC_Method := WCEM_Brackets;
1835       FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop);
1836
1837       if Start = 0 then
1838          File.WC_Method := WCEM_Brackets;
1839
1840       else
1841          if Stop = Start then
1842             for J in WC_Encoding_Letters'Range loop
1843                if File.Form (Start) = WC_Encoding_Letters (J) then
1844                   File.WC_Method := J;
1845                   return;
1846                end if;
1847             end loop;
1848          end if;
1849
1850          Close (File);
1851          raise Use_Error with "invalid WCEM form parameter";
1852       end if;
1853    end Set_WCEM;
1854
1855    ---------------
1856    -- Skip_Line --
1857    ---------------
1858
1859    procedure Skip_Line
1860      (File    : File_Type;
1861       Spacing : Positive_Count := 1)
1862    is
1863       ch : int;
1864
1865    begin
1866       --  Raise Constraint_Error if out of range value. The reason for this
1867       --  explicit test is that we don't want junk values around, even if
1868       --  checks are off in the caller.
1869
1870       if not Spacing'Valid then
1871          raise Constraint_Error;
1872       end if;
1873
1874       FIO.Check_Read_Status (AP (File));
1875
1876       for L in 1 .. Spacing loop
1877          if File.Before_LM then
1878             File.Before_LM := False;
1879
1880             --  Note that if File.Before_LM_PM is currently set, we also have
1881             --  to reset it (because it makes sense for Before_LM_PM to be set
1882             --  only when Before_LM is also set). This is done later on in this
1883             --  subprogram, as soon as Before_LM_PM has been taken into account
1884             --  for the purpose of page and line counts.
1885
1886          else
1887             ch := Getc (File);
1888
1889             --  If at end of file now, then immediately raise End_Error. Note
1890             --  that we can never be positioned between a line mark and a page
1891             --  mark, so if we are at the end of file, we cannot logically be
1892             --  before the implicit page mark that is at the end of the file.
1893
1894             --  For the same reason, we do not need an explicit check for a
1895             --  page mark. If there is a FF in the middle of a line, the file
1896             --  is not in canonical format and we do not care about the page
1897             --  numbers for files other than ones in canonical format.
1898
1899             if ch = EOF then
1900                raise End_Error;
1901             end if;
1902
1903             --  If not at end of file, then loop till we get to an LM or EOF.
1904             --  The latter case happens only in non-canonical files where the
1905             --  last line is not terminated by LM, but we don't want to blow
1906             --  up for such files, so we assume an implicit LM in this case.
1907
1908             loop
1909                exit when ch = LM or else ch = EOF;
1910                ch := Getc (File);
1911             end loop;
1912          end if;
1913
1914          --  We have got past a line mark, now, for a regular file only,
1915          --  see if a page mark immediately follows this line mark and
1916          --  if so, skip past the page mark as well. We do not do this
1917          --  for non-regular files, since it would cause an undesirable
1918          --  wait for an additional character.
1919
1920          File.Col := 1;
1921          File.Line := File.Line + 1;
1922
1923          if File.Before_LM_PM then
1924             File.Page := File.Page + 1;
1925             File.Line := 1;
1926             File.Before_LM_PM := False;
1927
1928          elsif File.Is_Regular_File then
1929             ch := Getc (File);
1930
1931             --  Page mark can be explicit, or implied at the end of the file
1932
1933             if (ch = PM or else ch = EOF)
1934               and then File.Is_Regular_File
1935             then
1936                File.Page := File.Page + 1;
1937                File.Line := 1;
1938             else
1939                Ungetc (ch, File);
1940             end if;
1941          end if;
1942       end loop;
1943
1944       File.Before_Upper_Half_Character := False;
1945    end Skip_Line;
1946
1947    procedure Skip_Line (Spacing : Positive_Count := 1) is
1948    begin
1949       Skip_Line (Current_In, Spacing);
1950    end Skip_Line;
1951
1952    ---------------
1953    -- Skip_Page --
1954    ---------------
1955
1956    procedure Skip_Page (File : File_Type) is
1957       ch : int;
1958
1959    begin
1960       FIO.Check_Read_Status (AP (File));
1961
1962       --  If at page mark already, just skip it
1963
1964       if File.Before_LM_PM then
1965          File.Before_LM := False;
1966          File.Before_LM_PM := False;
1967          File.Page := File.Page + 1;
1968          File.Line := 1;
1969          File.Col  := 1;
1970          return;
1971       end if;
1972
1973       --  This is a bit tricky, if we are logically before an LM then
1974       --  it is not an error if we are at an end of file now, since we
1975       --  are not really at it.
1976
1977       if File.Before_LM then
1978          File.Before_LM := False;
1979          File.Before_LM_PM := False;
1980          ch := Getc (File);
1981
1982       --  Otherwise we do raise End_Error if we are at the end of file now
1983
1984       else
1985          ch := Getc (File);
1986
1987          if ch = EOF then
1988             raise End_Error;
1989          end if;
1990       end if;
1991
1992       --  Now we can just rumble along to the next page mark, or to the
1993       --  end of file, if that comes first. The latter case happens when
1994       --  the page mark is implied at the end of file.
1995
1996       loop
1997          exit when ch = EOF
1998            or else (ch = PM and then File.Is_Regular_File);
1999          ch := Getc (File);
2000       end loop;
2001
2002       File.Page := File.Page + 1;
2003       File.Line := 1;
2004       File.Col  := 1;
2005       File.Before_Upper_Half_Character := False;
2006    end Skip_Page;
2007
2008    procedure Skip_Page is
2009    begin
2010       Skip_Page (Current_In);
2011    end Skip_Page;
2012
2013    --------------------
2014    -- Standard_Error --
2015    --------------------
2016
2017    function Standard_Error return File_Type is
2018    begin
2019       return Standard_Err;
2020    end Standard_Error;
2021
2022    function Standard_Error return File_Access is
2023    begin
2024       return Standard_Err'Access;
2025    end Standard_Error;
2026
2027    --------------------
2028    -- Standard_Input --
2029    --------------------
2030
2031    function Standard_Input return File_Type is
2032    begin
2033       return Standard_In;
2034    end Standard_Input;
2035
2036    function Standard_Input return File_Access is
2037    begin
2038       return Standard_In'Access;
2039    end Standard_Input;
2040
2041    ---------------------
2042    -- Standard_Output --
2043    ---------------------
2044
2045    function Standard_Output return File_Type is
2046    begin
2047       return Standard_Out;
2048    end Standard_Output;
2049
2050    function Standard_Output return File_Access is
2051    begin
2052       return Standard_Out'Access;
2053    end Standard_Output;
2054
2055    --------------------
2056    -- Terminate_Line --
2057    --------------------
2058
2059    procedure Terminate_Line (File : File_Type) is
2060    begin
2061       FIO.Check_File_Open (AP (File));
2062
2063       --  For file other than In_File, test for needing to terminate last line
2064
2065       if Mode (File) /= In_File then
2066
2067          --  If not at start of line definition need new line
2068
2069          if File.Col /= 1 then
2070             New_Line (File);
2071
2072          --  For files other than standard error and standard output, we
2073          --  make sure that an empty file has a single line feed, so that
2074          --  it is properly formatted. We avoid this for the standard files
2075          --  because it is too much of a nuisance to have these odd line
2076          --  feeds when nothing has been written to the file.
2077
2078          --  We also avoid this for files opened in append mode, in
2079          --  accordance with (RM A.8.2(10))
2080
2081          elsif (File /= Standard_Err and then File /= Standard_Out)
2082            and then (File.Line = 1 and then File.Page = 1)
2083            and then Mode (File) = Out_File
2084          then
2085             New_Line (File);
2086          end if;
2087       end if;
2088    end Terminate_Line;
2089
2090    ------------
2091    -- Ungetc --
2092    ------------
2093
2094    procedure Ungetc (ch : int; File : File_Type) is
2095    begin
2096       if ch /= EOF then
2097          if ungetc (ch, File.Stream) = EOF then
2098             raise Device_Error;
2099          end if;
2100       end if;
2101    end Ungetc;
2102
2103    -----------
2104    -- Write --
2105    -----------
2106
2107    --  This is the primitive Stream Write routine, used when a Text_IO file
2108    --  is treated directly as a stream using Text_IO.Streams.Stream.
2109
2110    procedure Write
2111      (File : in out Text_AFCB;
2112       Item : Stream_Element_Array)
2113    is
2114       pragma Warnings (Off, File);
2115       --  Because in this implementation we don't need IN OUT, we only read
2116
2117       function Has_Translated_Characters return Boolean;
2118       --  return True if Item array contains a character which will be
2119       --  translated under the text file mode. There is only one such
2120       --  character under DOS based systems which is character 10.
2121
2122       text_translation_required : Boolean;
2123       for text_translation_required'Size use Character'Size;
2124       pragma Import (C, text_translation_required,
2125                      "__gnat_text_translation_required");
2126
2127       Siz : constant size_t := Item'Length;
2128
2129       -------------------------------
2130       -- Has_Translated_Characters --
2131       -------------------------------
2132
2133       function Has_Translated_Characters return Boolean is
2134       begin
2135          for K in Item'Range loop
2136             if Item (K) = 10 then
2137                return True;
2138             end if;
2139          end loop;
2140          return False;
2141       end Has_Translated_Characters;
2142
2143       Needs_Binary_Write : constant Boolean :=
2144                              text_translation_required
2145                                and then Has_Translated_Characters;
2146
2147    --  Start of processing for Write
2148
2149    begin
2150       if File.Mode = FCB.In_File then
2151          raise Mode_Error;
2152       end if;
2153
2154       --  Now we do the write. Since this is a text file, it is normally in
2155       --  text mode, but stream data must be written in binary mode, so we
2156       --  temporarily set binary mode for the write, resetting it after. This
2157       --  is done only if needed (i.e. there is some characters in Item which
2158       --  needs to be written using the binary mode).
2159       --  These calls have no effect in a system (like Unix) where there is
2160       --  no distinction between text and binary files.
2161
2162       --  Since the character translation is done at the time the buffer is
2163       --  written (this is true under Windows) we first flush current buffer
2164       --  with text mode if needed.
2165
2166       if Needs_Binary_Write then
2167          if fflush (File.Stream) = -1 then
2168             raise Device_Error;
2169          end if;
2170
2171          set_binary_mode (fileno (File.Stream));
2172       end if;
2173
2174       if fwrite (Item'Address, 1, Siz, File.Stream) /= Siz then
2175          raise Device_Error;
2176       end if;
2177
2178       --  At this point we need to flush the buffer using the binary mode then
2179       --  we reset to text mode.
2180
2181       if Needs_Binary_Write then
2182          if fflush (File.Stream) = -1 then
2183             raise Device_Error;
2184          end if;
2185
2186          set_text_mode (fileno (File.Stream));
2187       end if;
2188    end Write;
2189
2190 begin
2191    --  Initialize Standard Files
2192
2193    for J in WC_Encoding_Method loop
2194       if WC_Encoding = WC_Encoding_Letters (J) then
2195          Default_WCEM := J;
2196       end if;
2197    end loop;
2198
2199    Initialize_Standard_Files;
2200
2201    FIO.Chain_File (AP (Standard_In));
2202    FIO.Chain_File (AP (Standard_Out));
2203    FIO.Chain_File (AP (Standard_Err));
2204
2205 end Ada.Text_IO;