1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with GNAT.OS_Lib; use GNAT.OS_Lib;
36 package body Output is
38 Current_FD : File_Descriptor := Standout;
39 -- File descriptor for current output
41 Special_Output_Proc : Output_Proc := null;
42 -- Record argument to last call to Set_Special_Output. If this is
43 -- non-null, then we are in special output mode.
45 -------------------------
46 -- Line Buffer Control --
47 -------------------------
49 -- Note: the following buffer and column position are maintained by
50 -- the subprograms defined in this package, and are not normally
51 -- directly modified or accessed by a client. However, a client is
52 -- permitted to modify these values, using the knowledge that only
53 -- Write_Eol actually generates any output.
55 Buffer_Max : constant := 8192;
56 Buffer : String (1 .. Buffer_Max + 1);
57 -- Buffer used to build output line. We do line buffering because it
58 -- is needed for the support of the debug-generated-code option (-gnatD).
59 -- Historically it was first added because on VMS, line buffering is
60 -- needed with certain file formats. So in any case line buffering must
61 -- be retained for this purpose, even if other reasons disappear. Note
62 -- any attempt to write more output to a line than can fit in the buffer
63 -- will be silently ignored.
65 Next_Column : Pos range 1 .. Buffer'Length + 1 := 1;
66 -- Column about to be written
68 -----------------------
69 -- Local_Subprograms --
70 -----------------------
72 procedure Flush_Buffer;
73 -- Flush buffer if non-empty and reset column counter
75 ---------------------------
76 -- Cancel_Special_Output --
77 ---------------------------
79 procedure Cancel_Special_Output is
81 Special_Output_Proc := null;
82 end Cancel_Special_Output;
88 procedure Flush_Buffer is
89 Len : constant Natural := Natural (Next_Column - 1);
94 -- If Special_Output_Proc has been set, then use it
96 if Special_Output_Proc /= null then
97 Special_Output_Proc.all (Buffer (1 .. Len));
99 -- If output is not set, then output to either standard output
100 -- or standard error.
102 elsif Len /= Write (Current_FD, Buffer'Address, Len) then
104 -- If there are errors with standard error, just quit
106 if Current_FD = Standerr then
109 -- Otherwise, set the output to standard error before
110 -- reporting a failure and quitting.
113 Current_FD := Standerr;
115 Write_Line ("fatal error: disk full");
120 -- Buffer is now empty
130 function Column return Nat is
135 ------------------------
136 -- Set_Special_Output --
137 ------------------------
139 procedure Set_Special_Output (P : Output_Proc) is
141 Special_Output_Proc := P;
142 end Set_Special_Output;
144 ------------------------
145 -- Set_Standard_Error --
146 ------------------------
148 procedure Set_Standard_Error is
150 if Special_Output_Proc = null then
155 Current_FD := Standerr;
156 end Set_Standard_Error;
158 -------------------------
159 -- Set_Standard_Output --
160 -------------------------
162 procedure Set_Standard_Output is
164 if Special_Output_Proc = null then
169 Current_FD := Standout;
170 end Set_Standard_Output;
176 procedure w (C : Character) is
184 procedure w (S : String) is
190 procedure w (V : Int) is
196 procedure w (B : Boolean) is
205 procedure w (L : String; C : Character) is
212 procedure w (L : String; S : String) is
219 procedure w (L : String; V : Int) is
226 procedure w (L : String; B : Boolean) is
237 procedure Write_Char (C : Character) is
239 if Next_Column = Buffer'Length then
243 Buffer (Natural (Next_Column)) := C;
244 Next_Column := Next_Column + 1;
251 procedure Write_Eol is
253 Buffer (Natural (Next_Column)) := ASCII.LF;
254 Next_Column := Next_Column + 1;
262 procedure Write_Int (Val : Int) is
270 Write_Int (Val / 10);
273 Write_Char (Character'Val ((Val mod 10) + Character'Pos ('0')));
281 procedure Write_Line (S : String) is
291 procedure Write_Str (S : String) is
293 for J in S'Range loop