OSDN Git Service

2007-04-20 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / output.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               O U T P U T                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, 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 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.                                              --
21 --                                                                          --
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.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with System.OS_Lib; use System.OS_Lib;
35
36 package body Output is
37
38    Current_FD : File_Descriptor := Standout;
39    --  File descriptor for current output
40
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.
44
45    -----------------------
46    -- Local_Subprograms --
47    -----------------------
48
49    procedure Flush_Buffer;
50    --  Flush buffer if non-empty and reset column counter
51
52    ---------------------------
53    -- Cancel_Special_Output --
54    ---------------------------
55
56    procedure Cancel_Special_Output is
57    begin
58       Special_Output_Proc := null;
59    end Cancel_Special_Output;
60
61    ------------
62    -- Column --
63    ------------
64
65    function Column return Pos is
66    begin
67       return Pos (Next_Col);
68    end Column;
69
70    ------------------
71    -- Flush_Buffer --
72    ------------------
73
74    procedure Flush_Buffer is
75       Len : constant Natural := Next_Col - 1;
76
77    begin
78       if Len /= 0 then
79
80          --  If Special_Output_Proc has been set, then use it
81
82          if Special_Output_Proc /= null then
83             Special_Output_Proc.all (Buffer (1 .. Len));
84
85          --  If output is not set, then output to either standard output
86          --  or standard error.
87
88          elsif Len /= Write (Current_FD, Buffer'Address, Len) then
89
90             --  If there are errors with standard error, just quit
91
92             if Current_FD = Standerr then
93                OS_Exit (2);
94
95             --  Otherwise, set the output to standard error before
96             --  reporting a failure and quitting.
97
98             else
99                Current_FD := Standerr;
100                Next_Col := 1;
101                Write_Line ("fatal error: disk full");
102                OS_Exit (2);
103             end if;
104          end if;
105
106          --  Buffer is now empty
107
108          Next_Col := 1;
109       end if;
110    end Flush_Buffer;
111
112    ---------------------------
113    -- Restore_Output_Buffer --
114    ---------------------------
115
116    procedure Restore_Output_Buffer (S : Saved_Output_Buffer) is
117    begin
118       Next_Col := S.Next_Col;
119       Buffer (1 .. Next_Col - 1) := S.Buffer (1 .. Next_Col - 1);
120    end Restore_Output_Buffer;
121
122    ------------------------
123    -- Save_Output_Buffer --
124    ------------------------
125
126    function Save_Output_Buffer return Saved_Output_Buffer is
127       S : Saved_Output_Buffer;
128    begin
129       S.Buffer (1 .. Next_Col - 1) := Buffer (1 .. Next_Col - 1);
130       S.Next_Col := Next_Col;
131       Next_Col := 1;
132       return S;
133    end Save_Output_Buffer;
134
135    ------------------------
136    -- Set_Special_Output --
137    ------------------------
138
139    procedure Set_Special_Output (P : Output_Proc) is
140    begin
141       Special_Output_Proc := P;
142    end Set_Special_Output;
143
144    ------------------------
145    -- Set_Standard_Error --
146    ------------------------
147
148    procedure Set_Standard_Error is
149    begin
150       if Special_Output_Proc = null then
151          Flush_Buffer;
152          Next_Col := 1;
153       end if;
154
155       Current_FD := Standerr;
156    end Set_Standard_Error;
157
158    -------------------------
159    -- Set_Standard_Output --
160    -------------------------
161
162    procedure Set_Standard_Output is
163    begin
164       if Special_Output_Proc = null then
165          Flush_Buffer;
166          Next_Col := 1;
167       end if;
168
169       Current_FD := Standout;
170    end Set_Standard_Output;
171
172    -------
173    -- w --
174    -------
175
176    procedure w (C : Character) is
177    begin
178       Write_Char (''');
179       Write_Char (C);
180       Write_Char (''');
181       Write_Eol;
182    end w;
183
184    procedure w (S : String) is
185    begin
186       Write_Str (S);
187       Write_Eol;
188    end w;
189
190    procedure w (V : Int) is
191    begin
192       Write_Int (V);
193       Write_Eol;
194    end w;
195
196    procedure w (B : Boolean) is
197    begin
198       if B then
199          w ("True");
200       else
201          w ("False");
202       end if;
203    end w;
204
205    procedure w (L : String; C : Character) is
206    begin
207       Write_Str (L);
208       Write_Char (' ');
209       w (C);
210    end w;
211
212    procedure w (L : String; S : String) is
213    begin
214       Write_Str (L);
215       Write_Char (' ');
216       w (S);
217    end w;
218
219    procedure w (L : String; V : Int) is
220    begin
221       Write_Str (L);
222       Write_Char (' ');
223       w (V);
224    end w;
225
226    procedure w (L : String; B : Boolean) is
227    begin
228       Write_Str (L);
229       Write_Char (' ');
230       w (B);
231    end w;
232
233    ----------------
234    -- Write_Char --
235    ----------------
236
237    procedure Write_Char (C : Character) is
238    begin
239       if Next_Col = Buffer'Length then
240          Write_Eol;
241       end if;
242
243       if C = ASCII.LF then
244          Write_Eol;
245       else
246          Buffer (Next_Col) := C;
247          Next_Col := Next_Col + 1;
248       end if;
249    end Write_Char;
250
251    ---------------
252    -- Write_Eol --
253    ---------------
254
255    procedure Write_Eol is
256    begin
257       Buffer (Next_Col) := ASCII.LF;
258       Next_Col := Next_Col + 1;
259       Flush_Buffer;
260    end Write_Eol;
261
262    ----------------------
263    -- Write_Erase_Char --
264    ----------------------
265
266    procedure Write_Erase_Char (C : Character) is
267    begin
268       if Next_Col /= 1 and then Buffer (Next_Col - 1) = C then
269          Next_Col := Next_Col - 1;
270       end if;
271    end Write_Erase_Char;
272
273    ---------------
274    -- Write_Int --
275    ---------------
276
277    procedure Write_Int (Val : Int) is
278    begin
279       if Val < 0 then
280          Write_Char ('-');
281          Write_Int (-Val);
282
283       else
284          if Val > 9 then
285             Write_Int (Val / 10);
286          end if;
287
288          Write_Char (Character'Val ((Val mod 10) + Character'Pos ('0')));
289       end if;
290    end Write_Int;
291
292    ----------------
293    -- Write_Line --
294    ----------------
295
296    procedure Write_Line (S : String) is
297    begin
298       Write_Str (S);
299       Write_Eol;
300    end Write_Line;
301
302    ------------------
303    -- Write_Spaces --
304    ------------------
305
306    procedure Write_Spaces (N : Nat) is
307    begin
308       for J in 1 .. N loop
309          Write_Char (' ');
310       end loop;
311    end Write_Spaces;
312
313    ---------------
314    -- Write_Str --
315    ---------------
316
317    procedure Write_Str (S : String) is
318    begin
319       for J in S'Range loop
320          Write_Char (S (J));
321       end loop;
322    end Write_Str;
323
324 end Output;