1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . C O M M A N D _ L I N E . R E S P O N S E _ F I L E --
9 -- Copyright (C) 2007-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 pragma Compiler_Unit_Warning
;
34 with Ada
.Unchecked_Deallocation
;
36 with System
.OS_Lib
; use System
.OS_Lib
;
38 package body Ada
.Command_Line
.Response_File
is
41 type File_Ptr
is access File_Rec
;
42 type File_Rec
is 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
60 function Arguments_From
61 (Response_File_Name
: String;
62 Recursive
: Boolean := False;
63 Ignore_Non_Existing_Files
: Boolean := False)
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
77 procedure Recurse
(File_Name
: String);
78 -- Get the arguments from the file and call itself recursively if one of
79 -- the argument starts with character '@'.
85 procedure Add_Argument
(Arg
: String) is
87 if Last_Arg
= Arguments
'Last then
89 New_Arguments
: constant Argument_List_Access
:=
90 new Argument_List
(1 .. Arguments
'Last * 2);
92 New_Arguments
(Arguments
'Range) := Arguments
.all;
93 Arguments
.all := (others => null);
95 Arguments
:= New_Arguments
;
99 Last_Arg
:= Last_Arg
+ 1;
100 Arguments
(Last_Arg
) := new String'(Arg);
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);
125 First_Char : Positive;
126 -- Index of the first character of an argument in Line
129 -- Index of the last character of an argument in Line
132 -- True when inside a quoted string
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
143 -- Get one line from the response file
149 function End_Of_File return Boolean is
151 return End_Of_File_Reached and then Buffer_Cursor > Buffer_Length;
158 procedure Get_Buffer is
160 Buffer_Length := Read (FD, Buffer (1)'Address, Buffer'Length);
161 End_Of_File_Reached := Buffer_Length < Buffer'Length;
169 procedure Get_Line is
180 Ch := Buffer (Buffer_Cursor);
182 exit when Ch = ASCII.CR or else
183 Ch = ASCII.LF or else
189 if Last = Line'Last then
193 Buffer_Cursor := Buffer_Cursor + 1;
195 if Buffer_Cursor > Buffer_Length then
205 Ch := Buffer (Buffer_Cursor);
207 exit when Ch /= ASCII.HT and then
208 Ch /= ASCII.LF and then
211 Buffer_Cursor := Buffer_Cursor + 1;
213 if Buffer_Cursor > Buffer_Length then
223 -- Start of processing for Recurse
228 if FD = Invalid_FD then
229 if Ignore_Non_Existing_Files then
232 raise File_Does_Not_Exist;
236 -- Put the response file name on the stack
238 if First_File = null then
241 (Name
=> new String'(File_Name),
244 Last_File := First_File;
248 Current : File_Ptr := First_File;
252 if Current.Name.all = File_Name then
253 raise Circularity_Detected;
256 Current := Current.Next;
257 exit when Current = null;
262 (Name
=> new String'(File_Name),
265 Last_File := Last_File.Next;
269 End_Of_File_Reached := False;
272 -- Read the response file line by line
275 while not End_Of_File loop
278 if Last = Line'Last then
284 -- Get each argument on the line
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;
296 exit Arg_Loop when First_Char > Last;
298 Last_Char := First_Char;
301 -- Get the character one by one
304 while Last_Char <= Last loop
306 -- Inside a string, check only for '"'
309 if Line (Last_Char) = '"' then
313 Line (Last_Char .. Last - 1) :=
314 Line (Last_Char + 1 .. Last);
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
325 Last_Char := Last_Char - 1;
329 -- If there are two consecutive '"', the quoted
330 -- string is not closed
332 In_String := Line (Last_Char) = '"';
335 Last_Char := Last_Char + 1;
340 Last_Char := Last_Char + 1;
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;
351 -- The argument ends with the line
356 elsif Line (Last_Char) = '"' then
358 -- Entering a quoted string: remove the '"'
361 Line (Last_Char .. Last - 1) :=
362 Line (Last_Char + 1 .. Last);
366 -- Outside quoted strings, white space ends the argument
369 when Line (Last_Char + 1) = ' ' or else
370 Line (Last_Char + 1) = ASCII.HT;
372 Last_Char := Last_Char + 1;
374 end loop Character_Loop;
376 -- It is an error to not close a quoted string before the end
380 raise No_Closing_Quote;
383 -- Add the argument to the list
386 Arg : String (1 .. Last_Char - First_Char + 1);
388 Arg := Line (First_Char .. Last_Char);
392 -- Next argument, if line is not finished
394 First_Char := Last_Char + 1;
400 -- If Recursive is True, check for any argument starting with '@'
404 while Arg <= Last_Arg loop
406 if Arguments (Arg)'Length > 0 and then
407 Arguments (Arg) (1) = '@'
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;
417 -- Save the current arguments and get those in the new
421 Inc_File_Name : constant String :=
422 Arguments (Arg) (2 .. Arguments (Arg)'Last);
423 Current_Arguments : constant Argument_List :=
424 Arguments (1 .. Last_Arg);
426 Recurse (Inc_File_Name);
428 -- Insert the new arguments where the new response
429 -- file was imported.
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;
439 -- Grow Arguments if it is not large enough
441 if Arguments'Last < New_Last_Arg then
442 Last_Arg := Arguments'Last;
445 while Last_Arg < New_Last_Arg loop
446 Last_Arg := Last_Arg * 2;
449 Arguments := new Argument_List (1 .. Last_Arg);
452 Last_Arg := New_Last_Arg;
454 Arguments (1 .. Last_Arg) :=
455 Current_Arguments (1 .. Arg - 1) &
458 (Arg + 1 .. Current_Arguments'Last);
460 Arg := Arg + New_Arguments'Length;
471 -- Remove the response file name from the stack
473 if First_File = Last_File then
474 System.Strings.Free (First_File.Name);
480 System.Strings.Free (Last_File.Name);
481 Last_File := Last_File.Prev;
482 Free (Last_File.Next);
492 -- Start of processing for Arguments_From
495 -- The job is done by procedure Recurse
497 Recurse (Response_File_Name);
499 -- Free Arguments before returning the result
502 Result : constant Argument_List := Arguments (1 .. Last_Arg);
511 -- When an exception occurs, deallocate everything
515 while First_File /= null loop
516 Last_File := First_File.Next;
517 System.Strings.Free (First_File.Name);
519 First_File := Last_File;
525 end Ada.Command_Line.Response_File;