* tree-vect-loop-manip.c (vect_do_peeling): Do not use
[official-gcc.git] / gcc / ada / libgnat / s-resfil.adb
blobb36ff944592bea3a6da70910242967e0d47c862e
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . R E S P O N S E _ F I L E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2007-2017, 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 ------------------------------------------------------------------------------
32 pragma Compiler_Unit_Warning;
34 with Ada.Unchecked_Deallocation;
36 with System.OS_Lib; use System.OS_Lib;
38 package body System.Response_File is
40 type File_Rec;
41 type File_Ptr is access File_Rec;
42 type File_Rec is record
43 Name : String_Access;
44 Next : File_Ptr;
45 Prev : File_Ptr;
46 end record;
47 -- To build a stack of response file names
49 procedure Free is new Ada.Unchecked_Deallocation (File_Rec, File_Ptr);
51 type Argument_List_Access is access Argument_List;
52 procedure Free is new Ada.Unchecked_Deallocation
53 (Argument_List, Argument_List_Access);
54 -- Free only the allocated Argument_List, not allocated String components
56 --------------------
57 -- Arguments_From --
58 --------------------
60 function Arguments_From
61 (Response_File_Name : String;
62 Recursive : Boolean := False;
63 Ignore_Non_Existing_Files : Boolean := False)
64 return Argument_List
66 First_File : File_Ptr := null;
67 Last_File : File_Ptr := null;
68 -- The stack of response files
70 Arguments : Argument_List_Access := new Argument_List (1 .. 4);
71 Last_Arg : Natural := 0;
73 procedure Add_Argument (Arg : String);
74 -- Add argument Arg to argument list Arguments, increasing Arguments
75 -- if necessary.
77 procedure Recurse (File_Name : String);
78 -- Get the arguments from the file and call itself recursively if one of
79 -- the arguments starts with character '@'.
81 ------------------
82 -- Add_Argument --
83 ------------------
85 procedure Add_Argument (Arg : String) is
86 begin
87 if Last_Arg = Arguments'Last then
88 declare
89 New_Arguments : constant Argument_List_Access :=
90 new Argument_List (1 .. Arguments'Last * 2);
91 begin
92 New_Arguments (Arguments'Range) := Arguments.all;
93 Arguments.all := (others => null);
94 Free (Arguments);
95 Arguments := New_Arguments;
96 end;
97 end if;
99 Last_Arg := Last_Arg + 1;
100 Arguments (Last_Arg) := new String'(Arg);
101 end Add_Argument;
103 -------------
104 -- Recurse --
105 -------------
107 procedure Recurse (File_Name : String) is
108 -- Open the response file. If not found, fail or report a warning,
109 -- depending on the value of Ignore_Non_Existing_Files.
111 FD : constant File_Descriptor := Open_Read (File_Name, Text);
113 Buffer_Size : constant := 1500;
114 Buffer : String (1 .. Buffer_Size);
116 Buffer_Length : Natural;
118 Buffer_Cursor : Natural;
120 End_Of_File_Reached : Boolean;
122 Line : String (1 .. Max_Line_Length + 1);
123 Last : Natural;
125 First_Char : Positive;
126 -- Index of the first character of an argument in Line
128 Last_Char : Natural;
129 -- Index of the last character of an argument in Line
131 In_String : Boolean;
132 -- True when inside a quoted string
134 Arg : Positive;
136 function End_Of_File return Boolean;
137 -- True when the end of the response file has been reached
139 procedure Get_Buffer;
140 -- Read one buffer from the response file
142 procedure Get_Line;
143 -- Get one line from the response file
145 -----------------
146 -- End_Of_File --
147 -----------------
149 function End_Of_File return Boolean is
150 begin
151 return End_Of_File_Reached and then Buffer_Cursor > Buffer_Length;
152 end End_Of_File;
154 ----------------
155 -- Get_Buffer --
156 ----------------
158 procedure Get_Buffer is
159 begin
160 Buffer_Length := Read (FD, Buffer (1)'Address, Buffer'Length);
161 End_Of_File_Reached := Buffer_Length < Buffer'Length;
162 Buffer_Cursor := 1;
163 end Get_Buffer;
165 --------------
166 -- Get_Line --
167 --------------
169 procedure Get_Line is
170 Ch : Character;
172 begin
173 Last := 0;
175 if End_Of_File then
176 return;
177 end if;
179 loop
180 Ch := Buffer (Buffer_Cursor);
182 exit when Ch = ASCII.CR or else
183 Ch = ASCII.LF or else
184 Ch = ASCII.FF;
186 Last := Last + 1;
187 Line (Last) := Ch;
189 if Last = Line'Last then
190 return;
191 end if;
193 Buffer_Cursor := Buffer_Cursor + 1;
195 if Buffer_Cursor > Buffer_Length then
196 Get_Buffer;
198 if End_Of_File then
199 return;
200 end if;
201 end if;
202 end loop;
204 loop
205 Ch := Buffer (Buffer_Cursor);
207 exit when Ch /= ASCII.HT and then
208 Ch /= ASCII.LF and then
209 Ch /= ASCII.FF;
211 Buffer_Cursor := Buffer_Cursor + 1;
213 if Buffer_Cursor > Buffer_Length then
214 Get_Buffer;
216 if End_Of_File then
217 return;
218 end if;
219 end if;
220 end loop;
221 end Get_Line;
223 -- Start of processing for Recurse
225 begin
226 Last_Arg := 0;
228 if FD = Invalid_FD then
229 if Ignore_Non_Existing_Files then
230 return;
231 else
232 raise File_Does_Not_Exist;
233 end if;
234 end if;
236 -- Put the response file name on the stack
238 if First_File = null then
239 First_File :=
240 new File_Rec'
241 (Name => new String'(File_Name),
242 Next => null,
243 Prev => null);
244 Last_File := First_File;
246 else
247 declare
248 Current : File_Ptr := First_File;
250 begin
251 loop
252 if Current.Name.all = File_Name then
253 raise Circularity_Detected;
254 end if;
256 Current := Current.Next;
257 exit when Current = null;
258 end loop;
260 Last_File.Next :=
261 new File_Rec'
262 (Name => new String'(File_Name),
263 Next => null,
264 Prev => Last_File);
265 Last_File := Last_File.Next;
266 end;
267 end if;
269 End_Of_File_Reached := False;
270 Get_Buffer;
272 -- Read the response file line by line
274 Line_Loop :
275 while not End_Of_File loop
276 Get_Line;
278 if Last = Line'Last then
279 raise Line_Too_Long;
280 end if;
282 First_Char := 1;
284 -- Get each argument on the line
286 Arg_Loop :
287 loop
288 -- First, skip any white space
290 while First_Char <= Last loop
291 exit when Line (First_Char) /= ' ' and then
292 Line (First_Char) /= ASCII.HT;
293 First_Char := First_Char + 1;
294 end loop;
296 exit Arg_Loop when First_Char > Last;
298 Last_Char := First_Char;
299 In_String := False;
301 -- Get the character one by one
303 Character_Loop :
304 while Last_Char <= Last loop
306 -- Inside a string, check only for '"'
308 if In_String then
309 if Line (Last_Char) = '"' then
311 -- Remove the '"'
313 Line (Last_Char .. Last - 1) :=
314 Line (Last_Char + 1 .. Last);
315 Last := Last - 1;
317 -- End of string is end of argument
319 if Last_Char > Last or else
320 Line (Last_Char) = ' ' or else
321 Line (Last_Char) = ASCII.HT
322 then
323 In_String := False;
325 Last_Char := Last_Char - 1;
326 exit Character_Loop;
328 else
329 -- If there are two consecutive '"', the quoted
330 -- string is not closed
332 In_String := Line (Last_Char) = '"';
334 if In_String then
335 Last_Char := Last_Char + 1;
336 end if;
337 end if;
339 else
340 Last_Char := Last_Char + 1;
341 end if;
343 elsif Last_Char = Last then
345 -- An opening '"' at the end of the line is an error
347 if Line (Last) = '"' then
348 raise No_Closing_Quote;
350 else
351 -- The argument ends with the line
353 exit Character_Loop;
354 end if;
356 elsif Line (Last_Char) = '"' then
358 -- Entering a quoted string: remove the '"'
360 In_String := True;
361 Line (Last_Char .. Last - 1) :=
362 Line (Last_Char + 1 .. Last);
363 Last := Last - 1;
365 else
366 -- Outside quoted strings, white space ends the argument
368 exit Character_Loop
369 when Line (Last_Char + 1) = ' ' or else
370 Line (Last_Char + 1) = ASCII.HT;
372 Last_Char := Last_Char + 1;
373 end if;
374 end loop Character_Loop;
376 -- It is an error to not close a quoted string before the end
377 -- of the line.
379 if In_String then
380 raise No_Closing_Quote;
381 end if;
383 -- Add the argument to the list
385 declare
386 Arg : String (1 .. Last_Char - First_Char + 1);
387 begin
388 Arg := Line (First_Char .. Last_Char);
389 Add_Argument (Arg);
390 end;
392 -- Next argument, if line is not finished
394 First_Char := Last_Char + 1;
395 end loop Arg_Loop;
396 end loop Line_Loop;
398 Close (FD);
400 -- If Recursive is True, check for any argument starting with '@'
402 if Recursive then
403 Arg := 1;
404 while Arg <= Last_Arg loop
406 if Arguments (Arg)'Length > 0 and then
407 Arguments (Arg) (1) = '@'
408 then
409 -- Ignore argument '@' with no file name
411 if Arguments (Arg)'Length = 1 then
412 Arguments (Arg .. Last_Arg - 1) :=
413 Arguments (Arg + 1 .. Last_Arg);
414 Last_Arg := Last_Arg - 1;
416 else
417 -- Save the current arguments and get those in the new
418 -- response file.
420 declare
421 Inc_File_Name : constant String :=
422 Arguments (Arg) (2 .. Arguments (Arg)'Last);
423 Current_Arguments : constant Argument_List :=
424 Arguments (1 .. Last_Arg);
425 begin
426 Recurse (Inc_File_Name);
428 -- Insert the new arguments where the new response
429 -- file was imported.
431 declare
432 New_Arguments : constant Argument_List :=
433 Arguments (1 .. Last_Arg);
434 New_Last_Arg : constant Positive :=
435 Current_Arguments'Length +
436 New_Arguments'Length - 1;
438 begin
439 -- Grow Arguments if it is not large enough
441 if Arguments'Last < New_Last_Arg then
442 Last_Arg := Arguments'Last;
443 Free (Arguments);
445 while Last_Arg < New_Last_Arg loop
446 Last_Arg := Last_Arg * 2;
447 end loop;
449 Arguments := new Argument_List (1 .. Last_Arg);
450 end if;
452 Last_Arg := New_Last_Arg;
454 Arguments (1 .. Last_Arg) :=
455 Current_Arguments (1 .. Arg - 1) &
456 New_Arguments &
457 Current_Arguments
458 (Arg + 1 .. Current_Arguments'Last);
460 Arg := Arg + New_Arguments'Length;
461 end;
462 end;
463 end if;
465 else
466 Arg := Arg + 1;
467 end if;
468 end loop;
469 end if;
471 -- Remove the response file name from the stack
473 if First_File = Last_File then
474 System.Strings.Free (First_File.Name);
475 Free (First_File);
476 First_File := null;
477 Last_File := null;
479 else
480 System.Strings.Free (Last_File.Name);
481 Last_File := Last_File.Prev;
482 Free (Last_File.Next);
483 end if;
485 exception
486 when others =>
487 Close (FD);
489 raise;
490 end Recurse;
492 -- Start of processing for Arguments_From
494 begin
495 -- The job is done by procedure Recurse
497 Recurse (Response_File_Name);
499 -- Free Arguments before returning the result
501 declare
502 Result : constant Argument_List := Arguments (1 .. Last_Arg);
503 begin
504 Free (Arguments);
505 return Result;
506 end;
508 exception
509 when others =>
511 -- When an exception occurs, deallocate everything
513 Free (Arguments);
515 while First_File /= null loop
516 Last_File := First_File.Next;
517 System.Strings.Free (First_File.Name);
518 Free (First_File);
519 First_File := Last_File;
520 end loop;
522 raise;
523 end Arguments_From;
525 end System.Response_File;