OSDN Git Service

2006-10-31 Robert Dewar <dewar@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-2005, 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 GNAT.OS_Lib; use GNAT.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    -- Flush_Buffer --
63    ------------------
64
65    procedure Flush_Buffer is
66       Len : constant Natural := Next_Col - 1;
67
68    begin
69       if Len /= 0 then
70
71          --  If Special_Output_Proc has been set, then use it
72
73          if Special_Output_Proc /= null then
74             Special_Output_Proc.all (Buffer (1 .. Len));
75
76          --  If output is not set, then output to either standard output
77          --  or standard error.
78
79          elsif Len /= Write (Current_FD, Buffer'Address, Len) then
80
81             --  If there are errors with standard error, just quit
82
83             if Current_FD = Standerr then
84                OS_Exit (2);
85
86             --  Otherwise, set the output to standard error before
87             --  reporting a failure and quitting.
88
89             else
90                Current_FD := Standerr;
91                Next_Col := 1;
92                Write_Line ("fatal error: disk full");
93                OS_Exit (2);
94             end if;
95          end if;
96
97          --  Buffer is now empty
98
99          Next_Col := 1;
100       end if;
101    end Flush_Buffer;
102
103    ------------
104    -- Column --
105    ------------
106
107    function Column return Pos is
108    begin
109       return Pos (Next_Col);
110    end Column;
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       Buffer (Next_Col) := C;
244       Next_Col := Next_Col + 1;
245    end Write_Char;
246
247    ---------------
248    -- Write_Eol --
249    ---------------
250
251    procedure Write_Eol is
252    begin
253       Buffer (Next_Col) := ASCII.LF;
254       Next_Col := Next_Col + 1;
255       Flush_Buffer;
256    end Write_Eol;
257
258    ----------------------
259    -- Write_Erase_Char --
260    ----------------------
261
262    procedure Write_Erase_Char (C : Character) is
263    begin
264       if Next_Col /= 1 and then Buffer (Next_Col - 1) = C then
265          Next_Col := Next_Col - 1;
266       end if;
267    end Write_Erase_Char;
268
269    ---------------
270    -- Write_Int --
271    ---------------
272
273    procedure Write_Int (Val : Int) is
274    begin
275       if Val < 0 then
276          Write_Char ('-');
277          Write_Int (-Val);
278
279       else
280          if Val > 9 then
281             Write_Int (Val / 10);
282          end if;
283
284          Write_Char (Character'Val ((Val mod 10) + Character'Pos ('0')));
285       end if;
286    end Write_Int;
287
288    ----------------
289    -- Write_Line --
290    ----------------
291
292    procedure Write_Line (S : String) is
293    begin
294       Write_Str (S);
295       Write_Eol;
296    end Write_Line;
297
298    ---------------
299    -- Write_Str --
300    ---------------
301
302    procedure Write_Str (S : String) is
303    begin
304       for J in S'Range loop
305          Write_Char (S (J));
306       end loop;
307    end Write_Str;
308
309 end Output;