1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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 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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 package body Output is
34 Buffer : String (1 .. Buffer_Max + 1) := (others => '*');
35 for Buffer'Alignment use 4;
36 -- Buffer used to build output line. We do line buffering because it is
37 -- needed for the support of the debug-generated-code option (-gnatD). Note
38 -- any attempt to write more output to a line than can fit in the buffer
39 -- will be silently ignored. The alignment clause improves the efficiency
40 -- of the save/restore procedures.
42 Next_Col : Positive range 1 .. Buffer'Length + 1 := 1;
43 -- Column about to be written
45 Current_FD : File_Descriptor := Standout;
46 -- File descriptor for current output
48 Special_Output_Proc : Output_Proc := null;
49 -- Record argument to last call to Set_Special_Output. If this is
50 -- non-null, then we are in special output mode.
52 Indentation_Amount : constant Positive := 3;
53 -- Number of spaces to output for each indentation level
55 Indentation_Limit : constant Positive := 40;
56 -- Indentation beyond this number of spaces wraps around
58 -- Disable the warnings emitted by -gnatwc because the comparison within
59 -- the assertion depends on conditional compilation.
61 pragma Warnings (Off, "condition can only be * if invalid values present");
62 pragma Assert (Indentation_Limit < Buffer_Max / 2);
63 pragma Warnings (On, "condition can only be * if invalid values present");
64 -- Make sure this is substantially shorter than the line length
66 Cur_Indentation : Natural := 0;
67 -- Number of spaces to indent each line
69 -----------------------
70 -- Local_Subprograms --
71 -----------------------
73 procedure Flush_Buffer;
74 -- Flush buffer if non-empty and reset column counter
76 ---------------------------
77 -- Cancel_Special_Output --
78 ---------------------------
80 procedure Cancel_Special_Output is
82 Special_Output_Proc := null;
83 end Cancel_Special_Output;
89 function Column return Pos is
91 return Pos (Next_Col);
94 ----------------------
95 -- Delete_Last_Char --
96 ----------------------
98 procedure Delete_Last_Char is
100 if Next_Col /= 1 then
101 Next_Col := Next_Col - 1;
103 end Delete_Last_Char;
109 procedure Flush_Buffer is
110 Write_Error : exception;
111 -- Raised if Write fails
117 procedure Write_Buffer (Buf : String);
118 -- Write out Buf, either using Special_Output_Proc, or the normal way
119 -- using Write. Raise Write_Error if Write fails (presumably due to disk
120 -- full). Write_Error is not used in the case of Special_Output_Proc.
122 procedure Write_Buffer (Buf : String) is
124 -- If Special_Output_Proc has been set, then use it
126 if Special_Output_Proc /= null then
127 Special_Output_Proc.all (Buf);
129 -- If output is not set, then output to either standard output
130 -- or standard error.
132 elsif Write (Current_FD, Buf'Address, Buf'Length) /= Buf'Length then
138 Len : constant Natural := Next_Col - 1;
140 -- Start of processing for Flush_Buffer
145 -- If there's no indentation, or if the line is too long with
146 -- indentation, or if it's a blank line, just write the buffer.
148 if Cur_Indentation = 0
149 or else Cur_Indentation + Len > Buffer_Max
150 or else Buffer (1 .. Len) = (1 => ASCII.LF)
152 Write_Buffer (Buffer (1 .. Len));
154 -- Otherwise, construct a new buffer with preceding spaces, and
159 Indented_Buffer : constant String :=
160 (1 .. Cur_Indentation => ' ') &
163 Write_Buffer (Indented_Buffer);
170 -- If there are errors with standard error just quit. Otherwise
171 -- set the output to standard error before reporting a failure
174 if Current_FD /= Standerr then
175 Current_FD := Standerr;
177 Write_Line ("fatal error: disk full");
183 -- Buffer is now empty
193 procedure Ignore_Output (S : String) is
204 -- The "mod" in the following assignment is to cause a wrap around in
205 -- the case where there is too much indentation.
208 (Cur_Indentation + Indentation_Amount) mod Indentation_Limit;
215 function Last_Char return Character is
217 if Next_Col /= 1 then
218 return Buffer (Next_Col - 1);
230 -- The "mod" here undoes the wrap around from Indent above
233 (Cur_Indentation - Indentation_Amount) mod Indentation_Limit;
236 ---------------------------
237 -- Restore_Output_Buffer --
238 ---------------------------
240 procedure Restore_Output_Buffer (S : Saved_Output_Buffer) is
242 Next_Col := S.Next_Col;
243 Cur_Indentation := S.Cur_Indentation;
244 Buffer (1 .. Next_Col - 1) := S.Buffer (1 .. Next_Col - 1);
245 end Restore_Output_Buffer;
247 ------------------------
248 -- Save_Output_Buffer --
249 ------------------------
251 function Save_Output_Buffer return Saved_Output_Buffer is
252 S : Saved_Output_Buffer;
254 S.Buffer (1 .. Next_Col - 1) := Buffer (1 .. Next_Col - 1);
255 S.Next_Col := Next_Col;
256 S.Cur_Indentation := Cur_Indentation;
258 Cur_Indentation := 0;
260 end Save_Output_Buffer;
262 ------------------------
263 -- Set_Special_Output --
264 ------------------------
266 procedure Set_Special_Output (P : Output_Proc) is
268 Special_Output_Proc := P;
269 end Set_Special_Output;
275 procedure Set_Output (FD : File_Descriptor) is
277 if Special_Output_Proc = null then
284 ------------------------
285 -- Set_Standard_Error --
286 ------------------------
288 procedure Set_Standard_Error is
290 Set_Output (Standerr);
291 end Set_Standard_Error;
293 -------------------------
294 -- Set_Standard_Output --
295 -------------------------
297 procedure Set_Standard_Output is
299 Set_Output (Standout);
300 end Set_Standard_Output;
306 procedure w (C : Character) is
314 procedure w (S : String) is
320 procedure w (V : Int) is
326 procedure w (B : Boolean) is
335 procedure w (L : String; C : Character) is
342 procedure w (L : String; S : String) is
349 procedure w (L : String; V : Int) is
356 procedure w (L : String; B : Boolean) is
367 procedure Write_Char (C : Character) is
369 pragma Assert (Next_Col in Buffer'Range);
370 if Next_Col = Buffer'Length then
377 Buffer (Next_Col) := C;
378 Next_Col := Next_Col + 1;
386 procedure Write_Eol is
388 -- Remove any trailing spaces
390 while Next_Col > 1 and then Buffer (Next_Col - 1) = ' ' loop
391 Next_Col := Next_Col - 1;
394 Buffer (Next_Col) := ASCII.LF;
395 Next_Col := Next_Col + 1;
399 ---------------------------
400 -- Write_Eol_Keep_Blanks --
401 ---------------------------
403 procedure Write_Eol_Keep_Blanks is
405 Buffer (Next_Col) := ASCII.LF;
406 Next_Col := Next_Col + 1;
408 end Write_Eol_Keep_Blanks;
410 ----------------------
411 -- Write_Erase_Char --
412 ----------------------
414 procedure Write_Erase_Char (C : Character) is
416 if Next_Col /= 1 and then Buffer (Next_Col - 1) = C then
417 Next_Col := Next_Col - 1;
419 end Write_Erase_Char;
425 procedure Write_Int (Val : Int) is
426 -- Type Int has one extra negative number (i.e. two's complement), so we
427 -- work with negative numbers here. Otherwise, negating Int'First will
430 subtype Nonpositive is Int range Int'First .. 0;
431 procedure Write_Abs (Val : Nonpositive);
432 -- Write out the absolute value of Val
434 procedure Write_Abs (Val : Nonpositive) is
437 Write_Abs (Val / 10); -- Recursively write higher digits
440 Write_Char (Character'Val (-(Val rem 10) + Character'Pos ('0')));
456 procedure Write_Line (S : String) is
466 procedure Write_Spaces (N : Nat) is
477 procedure Write_Str (S : String) is
479 for J in S'Range loop