1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . C O M M A N D _ L I N E --
9 -- Copyright (C) 1999-2008, 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 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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada
.Unchecked_Deallocation
;
35 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
37 package body GNAT
.Command_Line
is
39 package CL
renames Ada
.Command_Line
;
41 type Switch_Parameter_Type
is
43 Parameter_With_Optional_Space
, -- ':' in getopt
44 Parameter_With_Space_Or_Equal
, -- '=' in getopt
45 Parameter_No_Space
, -- '!' in getopt
46 Parameter_Optional
); -- '?' in getopt
48 procedure Set_Parameter
49 (Variable
: out Parameter_Type
;
53 Extra
: Character := ASCII
.NUL
);
54 pragma Inline
(Set_Parameter
);
55 -- Set the parameter that will be returned by Parameter below
56 -- Parameters need to be defined ???
58 function Goto_Next_Argument_In_Section
(Parser
: Opt_Parser
) return Boolean;
59 -- Go to the next argument on the command line. If we are at the end of
60 -- the current section, we want to make sure there is no other identical
61 -- section on the command line (there might be multiple instances of
62 -- -largs). Returns True iff there is another argument.
64 function Get_File_Names_Case_Sensitive
return Integer;
65 pragma Import
(C
, Get_File_Names_Case_Sensitive
,
66 "__gnat_get_file_names_case_sensitive");
68 File_Names_Case_Sensitive
: constant Boolean :=
69 Get_File_Names_Case_Sensitive
/= 0;
71 procedure Canonical_Case_File_Name
(S
: in out String);
72 -- Given a file name, converts it to canonical case form. For systems where
73 -- file names are case sensitive, this procedure has no effect. If file
74 -- names are not case sensitive (i.e. for example if you have the file
75 -- "xyz.adb", you can refer to it as XYZ.adb or XyZ.AdB), then this call
76 -- converts the given string to canonical all lower case form, so that two
77 -- file names compare equal if they refer to the same file.
79 procedure Internal_Initialize_Option_Scan
81 Switch_Char
: Character;
82 Stop_At_First_Non_Switch
: Boolean;
83 Section_Delimiters
: String);
84 -- Initialize Parser, which must have been allocated already
86 function Argument
(Parser
: Opt_Parser
; Index
: Integer) return String;
87 -- Return the index-th command line argument
89 procedure Find_Longest_Matching_Switch
92 Index_In_Switches
: out Integer;
93 Switch_Length
: out Integer;
94 Param
: out Switch_Parameter_Type
);
95 -- return the Longest switch from Switches that matches at least
96 -- partially Arg. Index_In_Switches is set to 0 if none matches
98 procedure Unchecked_Free
is new Ada
.Unchecked_Deallocation
99 (Argument_List
, Argument_List_Access
);
101 procedure Unchecked_Free
is new Ada
.Unchecked_Deallocation
102 (Command_Line_Configuration_Record
, Command_Line_Configuration
);
104 type Boolean_Chars
is array (Character) of Boolean;
106 procedure Remove
(Line
: in out Argument_List_Access
; Index
: Integer);
107 -- Remove a specific element from Line
110 (Line
: in out Argument_List_Access
;
111 Str
: String_Access
);
112 -- Append a new element to Line
114 function Args_From_Expanded
(Args
: Boolean_Chars
) return String;
115 -- Return the string made of all characters with True in Args
118 with procedure Callback
(Simple_Switch
: String);
119 procedure For_Each_Simple_Switch
122 -- Breaks Switch into as simple switches as possible (expanding aliases and
123 -- ungrouping common prefixes when possible), and call Callback for each of
126 procedure Group_Switches
128 Result
: Argument_List_Access
;
129 Params
: Argument_List_Access
);
130 -- Group switches with common prefixes whenever possible.
131 -- Once they have been grouped, we also check items for possible aliasing
133 procedure Alias_Switches
135 Result
: Argument_List_Access
;
136 Params
: Argument_List_Access
);
137 -- When possible, replace or more switches by an alias, i.e. a shorter
143 Substring
: String) return Boolean;
144 -- Return True if the characters starting at Index in Type_Str are
145 -- equivalent to Substring.
151 function Argument
(Parser
: Opt_Parser
; Index
: Integer) return String is
153 if Parser
.Arguments
/= null then
154 return Parser
.Arguments
(Index
+ Parser
.Arguments
'First - 1).all;
156 return CL
.Argument
(Index
);
160 ------------------------------
161 -- Canonical_Case_File_Name --
162 ------------------------------
164 procedure Canonical_Case_File_Name
(S
: in out String) is
166 if not File_Names_Case_Sensitive
then
167 for J
in S
'Range loop
168 if S
(J
) in 'A' .. 'Z' then
169 S
(J
) := Character'Val
170 (Character'Pos (S
(J
)) +
171 Character'Pos ('a') -
172 Character'Pos ('A'));
176 end Canonical_Case_File_Name
;
182 function Expansion
(Iterator
: Expansion_Iterator
) return String is
183 use GNAT
.Directory_Operations
;
184 type Pointer
is access all Expansion_Iterator
;
186 It
: constant Pointer
:= Iterator
'Unrestricted_Access;
187 S
: String (1 .. 1024);
190 Current
: Depth
:= It
.Current_Depth
;
194 -- It is assumed that a directory is opened at the current level.
195 -- Otherwise GNAT.Directory_Operations.Directory_Error will be raised
196 -- at the first call to Read.
199 Read
(It
.Levels
(Current
).Dir
, S
, Last
);
201 -- If we have exhausted the directory, close it and go back one level
204 Close
(It
.Levels
(Current
).Dir
);
206 -- If we are at level 1, we are finished; return an empty string
209 return String'(1 .. 0 => ' ');
211 -- Otherwise continue with the directory at the previous level
213 Current := Current - 1;
214 It.Current_Depth := Current;
217 -- If this is a directory, that is neither "." or "..", attempt to
218 -- go to the next level.
221 (It.Dir_Name (1 .. It.Levels (Current).Name_Last) & S (1 .. Last))
222 and then S (1 .. Last) /= "."
223 and then S (1 .. Last) /= ".."
225 -- We can go to the next level only if we have not reached the
228 if Current < It.Maximum_Depth then
229 NL := It.Levels (Current).Name_Last;
231 -- And if relative path of this new directory is not too long
233 if NL + Last + 1 < Max_Path_Length then
234 Current := Current + 1;
235 It.Current_Depth := Current;
236 It.Dir_Name (NL + 1 .. NL + Last) := S (1 .. Last);
238 It.Dir_Name (NL) := Directory_Separator;
239 It.Levels (Current).Name_Last := NL;
240 Canonical_Case_File_Name (It.Dir_Name (1 .. NL));
242 -- Open the new directory, and read from it
244 GNAT.Directory_Operations.Open
245 (It.Levels (Current).Dir, It.Dir_Name (1 .. NL));
249 -- If not a directory, check the relative path against the pattern
254 It.Dir_Name (It.Start .. It.Levels (Current).Name_Last)
257 Canonical_Case_File_Name (Name);
259 -- If it matches return the relative path
261 if GNAT.Regexp.Match (Name, Iterator.Regexp) then
274 (Parser : Opt_Parser := Command_Line_Parser) return String
277 if Parser.The_Switch.Extra = ASCII.NUL then
278 return Argument (Parser, Parser.The_Switch.Arg_Num)
279 (Parser.The_Switch.First .. Parser.The_Switch.Last);
281 return Parser.The_Switch.Extra
282 & Argument (Parser, Parser.The_Switch.Arg_Num)
283 (Parser.The_Switch.First .. Parser.The_Switch.Last);
291 function Get_Argument
292 (Do_Expansion : Boolean := False;
293 Parser : Opt_Parser := Command_Line_Parser) return String
296 if Parser.In_Expansion then
298 S : constant String := Expansion (Parser.Expansion_It);
300 if S'Length /= 0 then
303 Parser.In_Expansion := False;
308 if Parser.Current_Argument > Parser.Arg_Count then
310 -- If this is the first time this function is called
312 if Parser.Current_Index = 1 then
313 Parser.Current_Argument := 1;
314 while Parser.Current_Argument <= Parser.Arg_Count
315 and then Parser.Section (Parser.Current_Argument) /=
316 Parser.Current_Section
318 Parser.Current_Argument := Parser.Current_Argument + 1;
321 return String'(1 .. 0 => ' ');
324 elsif Parser
.Section
(Parser
.Current_Argument
) = 0 then
325 while Parser
.Current_Argument
<= Parser
.Arg_Count
326 and then Parser
.Section
(Parser
.Current_Argument
) /=
327 Parser
.Current_Section
329 Parser
.Current_Argument
:= Parser
.Current_Argument
+ 1;
333 Parser
.Current_Index
:= Integer'Last;
335 while Parser
.Current_Argument
<= Parser
.Arg_Count
336 and then Parser
.Is_Switch
(Parser
.Current_Argument
)
338 Parser
.Current_Argument
:= Parser
.Current_Argument
+ 1;
341 if Parser
.Current_Argument
> Parser
.Arg_Count
then
342 return String'(1 .. 0 => ' ');
343 elsif Parser.Section (Parser.Current_Argument) = 0 then
344 return Get_Argument (Do_Expansion);
347 Parser.Current_Argument := Parser.Current_Argument + 1;
349 -- Could it be a file name with wild cards to expand?
353 Arg : constant String :=
354 Argument (Parser, Parser.Current_Argument - 1);
359 while Index <= Arg'Last loop
361 or else Arg (Index) = '?
'
362 or else Arg (Index) = '['
364 Parser.In_Expansion := True;
365 Start_Expansion (Parser.Expansion_It, Arg);
366 return Get_Argument (Do_Expansion);
374 return Argument (Parser, Parser.Current_Argument - 1);
377 ----------------------------------
378 -- Find_Longest_Matching_Switch --
379 ----------------------------------
381 procedure Find_Longest_Matching_Switch
384 Index_In_Switches : out Integer;
385 Switch_Length : out Integer;
386 Param : out Switch_Parameter_Type)
389 Length : Natural := 1;
390 P : Switch_Parameter_Type;
393 Index_In_Switches := 0;
396 -- Remove all leading spaces first to make sure that Index points
397 -- at the start of the first switch.
399 Index := Switches'First;
400 while Index <= Switches'Last and then Switches (Index) = ' ' loop
404 while Index <= Switches'Last loop
406 -- Search the length of the parameter at this position in Switches
409 while Length <= Switches'Last
410 and then Switches (Length) /= ' '
412 Length := Length + 1;
415 if Length = Index + 1 then
418 case Switches (Length - 1) is
420 P := Parameter_With_Optional_Space;
421 Length := Length - 1;
423 P := Parameter_With_Space_Or_Equal;
424 Length := Length - 1;
426 P := Parameter_No_Space;
427 Length := Length - 1;
429 P := Parameter_Optional;
430 Length := Length - 1;
436 -- If it is the one we searched, it may be a candidate
438 if Arg'First + Length - 1 - Index <= Arg'Last
439 and then Switches (Index .. Length - 1) =
440 Arg (Arg'First .. Arg'First + Length - 1 - Index)
441 and then Length - Index > Switch_Length
444 Index_In_Switches := Index;
445 Switch_Length := Length - Index;
448 -- Look for the next switch in Switches
450 while Index <= Switches'Last
451 and then Switches (Index) /= ' '
458 end Find_Longest_Matching_Switch;
466 Concatenate : Boolean := True;
467 Parser : Opt_Parser := Command_Line_Parser) return Character
470 pragma Unreferenced (Dummy);
475 -- If we have finished parsing the current command line item (there
476 -- might be multiple switches in a single item), then go to the next
479 if Parser.Current_Argument > Parser.Arg_Count
480 or else (Parser.Current_Index >
481 Argument (Parser, Parser.Current_Argument)'Last
482 and then not Goto_Next_Argument_In_Section (Parser))
487 -- By default, the switch will not have a parameter
489 Parser.The_Parameter :=
490 (Integer'Last, Integer'Last, Integer'Last - 1, ASCII.NUL);
491 Parser.The_Separator := ASCII.NUL;
494 Arg : constant String :=
495 Argument (Parser, Parser.Current_Argument);
496 Index_Switches : Natural := 0;
497 Max_Length : Natural := 0;
499 Param : Switch_Parameter_Type;
501 -- If we are on a new item, test if this might be a switch
503 if Parser.Current_Index = Arg'First then
504 if Arg (Arg'First) /= Parser.Switch_Character then
506 -- If it isn't a switch, return it immediately. We also know it
507 -- isn't the parameter to a previous switch, since that has
508 -- already been handled
510 if Switches (Switches'First) = '*' then
513 Arg_Num => Parser.Current_Argument,
516 Parser.Is_Switch (Parser.Current_Argument) := True;
517 Dummy := Goto_Next_Argument_In_Section (Parser);
521 if Parser.Stop_At_First then
522 Parser.Current_Argument := Positive'Last;
525 elsif not Goto_Next_Argument_In_Section (Parser) then
529 -- Recurse to get the next switch on the command line
535 -- We are on the first character of a new command line argument,
536 -- which starts with Switch_Character. Further analysis is needed.
538 Parser.Current_Index := Parser.Current_Index + 1;
539 Parser.Is_Switch (Parser.Current_Argument) := True;
542 Find_Longest_Matching_Switch
543 (Switches => Switches,
544 Arg => Arg (Parser.Current_Index .. Arg'Last),
545 Index_In_Switches => Index_Switches,
546 Switch_Length => Max_Length,
549 -- If switch is not accepted, it is either invalid or is returned
550 -- in the context of '*'.
552 if Index_Switches = 0 then
554 -- Depending on the value of Concatenate, the full switch is
555 -- a single character or the rest of the argument.
558 End_Index := Parser.Current_Index;
560 End_Index := Arg'Last;
563 if Switches (Switches'First) = '*' then
565 -- Always prepend the switch character, so that users know that
566 -- this comes from a switch on the command line. This is
567 -- especially important when Concatenate is False, since
568 -- otherwise the current argument first character is lost.
572 Arg_Num => Parser.Current_Argument,
573 First => Parser.Current_Index,
575 Extra => Parser.Switch_Character);
576 Parser.Is_Switch (Parser.Current_Argument) := True;
577 Dummy := Goto_Next_Argument_In_Section (Parser);
583 Arg_Num => Parser.Current_Argument,
584 First => Parser.Current_Index,
586 Parser.Current_Index := End_Index + 1;
587 raise Invalid_Switch;
590 End_Index := Parser.Current_Index + Max_Length - 1;
593 Arg_Num => Parser.Current_Argument,
594 First => Parser.Current_Index,
598 when Parameter_With_Optional_Space =>
599 if End_Index < Arg'Last then
601 (Parser.The_Parameter,
602 Arg_Num => Parser.Current_Argument,
603 First => End_Index + 1,
605 Dummy := Goto_Next_Argument_In_Section (Parser);
607 elsif Parser.Current_Argument < Parser.Arg_Count
608 and then Parser.Section (Parser.Current_Argument + 1) /= 0
610 Parser.Current_Argument := Parser.Current_Argument + 1;
611 Parser.The_Separator := ' ';
613 (Parser.The_Parameter,
614 Arg_Num => Parser.Current_Argument,
615 First => Argument (Parser, Parser.Current_Argument)'First,
616 Last => Argument (Parser, Parser.Current_Argument)'Last);
617 Parser.Is_Switch (Parser.Current_Argument) := True;
618 Dummy := Goto_Next_Argument_In_Section (Parser);
621 Parser.Current_Index := End_Index + 1;
622 raise Invalid_Parameter;
625 when Parameter_With_Space_Or_Equal =>
627 -- If the switch is of the form <switch>=xxx
629 if End_Index < Arg'Last then
631 if Arg (End_Index + 1) = '='
632 and then End_Index + 1 < Arg'Last
634 Parser.The_Separator := '=';
636 (Parser.The_Parameter,
637 Arg_Num => Parser.Current_Argument,
638 First => End_Index + 2,
640 Dummy := Goto_Next_Argument_In_Section (Parser);
642 Parser.Current_Index := End_Index + 1;
643 raise Invalid_Parameter;
646 -- If the switch is of the form <switch> xxx
648 elsif Parser.Current_Argument < Parser.Arg_Count
649 and then Parser.Section (Parser.Current_Argument + 1) /= 0
651 Parser.Current_Argument := Parser.Current_Argument + 1;
652 Parser.The_Separator := ' ';
654 (Parser.The_Parameter,
655 Arg_Num => Parser.Current_Argument,
656 First => Argument (Parser, Parser.Current_Argument)'First,
657 Last => Argument (Parser, Parser.Current_Argument)'Last);
658 Parser.Is_Switch (Parser.Current_Argument) := True;
659 Dummy := Goto_Next_Argument_In_Section (Parser);
662 Parser.Current_Index := End_Index + 1;
663 raise Invalid_Parameter;
666 when Parameter_No_Space =>
668 if End_Index < Arg'Last then
670 (Parser.The_Parameter,
671 Arg_Num => Parser.Current_Argument,
672 First => End_Index + 1,
674 Dummy := Goto_Next_Argument_In_Section (Parser);
677 Parser.Current_Index := End_Index + 1;
678 raise Invalid_Parameter;
681 when Parameter_Optional =>
683 if End_Index < Arg'Last then
685 (Parser.The_Parameter,
686 Arg_Num => Parser.Current_Argument,
687 First => End_Index + 1,
691 Dummy := Goto_Next_Argument_In_Section (Parser);
693 when Parameter_None =>
695 if Concatenate or else End_Index = Arg'Last then
696 Parser.Current_Index := End_Index + 1;
699 -- If Concatenate is False and the full argument is not
700 -- recognized as a switch, this is an invalid switch.
702 if Switches (Switches'First) = '*' then
705 Arg_Num => Parser.Current_Argument,
708 Parser.Is_Switch (Parser.Current_Argument) := True;
709 Dummy := Goto_Next_Argument_In_Section (Parser);
715 Arg_Num => Parser.Current_Argument,
716 First => Parser.Current_Index,
718 Parser.Current_Index := Arg'Last + 1;
719 raise Invalid_Switch;
723 return Switches (Index_Switches);
727 -----------------------------------
728 -- Goto_Next_Argument_In_Section --
729 -----------------------------------
731 function Goto_Next_Argument_In_Section
732 (Parser : Opt_Parser) return Boolean
735 Parser.Current_Argument := Parser.Current_Argument + 1;
737 if Parser.Current_Argument > Parser.Arg_Count
738 or else Parser.Section (Parser.Current_Argument) = 0
741 Parser.Current_Argument := Parser.Current_Argument + 1;
743 if Parser.Current_Argument > Parser.Arg_Count then
744 Parser.Current_Index := 1;
748 exit when Parser.Section (Parser.Current_Argument) =
749 Parser.Current_Section;
753 Parser.Current_Index :=
754 Argument (Parser, Parser.Current_Argument)'First;
757 end Goto_Next_Argument_In_Section;
763 procedure Goto_Section
764 (Name : String := "";
765 Parser : Opt_Parser := Command_Line_Parser)
770 Parser.In_Expansion := False;
773 Parser.Current_Argument := 1;
774 Parser.Current_Index := 1;
775 Parser.Current_Section := 1;
780 while Index <= Parser.Arg_Count loop
781 if Parser.Section (Index) = 0
782 and then Argument (Parser, Index) = Parser.Switch_Character & Name
784 Parser.Current_Argument := Index + 1;
785 Parser.Current_Index := 1;
787 if Parser.Current_Argument <= Parser.Arg_Count then
788 Parser.Current_Section :=
789 Parser.Section (Parser.Current_Argument);
797 Parser.Current_Argument := Positive'Last;
798 Parser.Current_Index := 2; -- so that Get_Argument returns nothing
801 ----------------------------
802 -- Initialize_Option_Scan --
803 ----------------------------
805 procedure Initialize_Option_Scan
806 (Switch_Char : Character := '-';
807 Stop_At_First_Non_Switch : Boolean := False;
808 Section_Delimiters : String := "")
811 Internal_Initialize_Option_Scan
812 (Parser => Command_Line_Parser,
813 Switch_Char => Switch_Char,
814 Stop_At_First_Non_Switch => Stop_At_First_Non_Switch,
815 Section_Delimiters => Section_Delimiters);
816 end Initialize_Option_Scan;
818 ----------------------------
819 -- Initialize_Option_Scan --
820 ----------------------------
822 procedure Initialize_Option_Scan
823 (Parser : out Opt_Parser;
824 Command_Line : GNAT.OS_Lib.Argument_List_Access;
825 Switch_Char : Character := '-';
826 Stop_At_First_Non_Switch : Boolean := False;
827 Section_Delimiters : String := "")
832 if Command_Line = null then
833 Parser := new Opt_Parser_Data (CL.Argument_Count);
834 Initialize_Option_Scan
835 (Switch_Char => Switch_Char,
836 Stop_At_First_Non_Switch => Stop_At_First_Non_Switch,
837 Section_Delimiters => Section_Delimiters);
839 Parser := new Opt_Parser_Data (Command_Line'Length);
840 Parser.Arguments := Command_Line;
841 Internal_Initialize_Option_Scan
843 Switch_Char => Switch_Char,
844 Stop_At_First_Non_Switch => Stop_At_First_Non_Switch,
845 Section_Delimiters => Section_Delimiters);
847 end Initialize_Option_Scan;
849 -------------------------------------
850 -- Internal_Initialize_Option_Scan --
851 -------------------------------------
853 procedure Internal_Initialize_Option_Scan
854 (Parser : Opt_Parser;
855 Switch_Char : Character;
856 Stop_At_First_Non_Switch : Boolean;
857 Section_Delimiters : String)
859 Section_Num : Section_Number;
860 Section_Index : Integer;
862 Delimiter_Found : Boolean;
865 pragma Warnings (Off, Discard);
868 Parser.Current_Argument := 0;
869 Parser.Current_Index := 0;
870 Parser.In_Expansion := False;
871 Parser.Switch_Character := Switch_Char;
872 Parser.Stop_At_First := Stop_At_First_Non_Switch;
874 -- If we are using sections, we have to preprocess the command line
875 -- to delimit them. A section can be repeated, so we just give each
876 -- item on the command line a section number
879 Section_Index := Section_Delimiters'First;
880 while Section_Index <= Section_Delimiters'Last loop
881 Last := Section_Index;
882 while Last <= Section_Delimiters'Last
883 and then Section_Delimiters (Last) /= ' '
888 Delimiter_Found := False;
889 Section_Num := Section_Num + 1;
891 for Index in 1 .. Parser.Arg_Count loop
892 if Argument (Parser, Index)(1) = Parser.Switch_Character
894 Argument (Parser, Index) = Parser.Switch_Character &
896 (Section_Index .. Last - 1)
898 Parser.Section (Index) := 0;
899 Delimiter_Found := True;
901 elsif Parser.Section (Index) = 0 then
902 Delimiter_Found := False;
904 elsif Delimiter_Found then
905 Parser.Section (Index) := Section_Num;
909 Section_Index := Last + 1;
910 while Section_Index <= Section_Delimiters'Last
911 and then Section_Delimiters (Section_Index) = ' '
913 Section_Index := Section_Index + 1;
917 Discard := Goto_Next_Argument_In_Section (Parser);
918 end Internal_Initialize_Option_Scan;
925 (Parser : Opt_Parser := Command_Line_Parser) return String
928 if Parser.The_Parameter.First > Parser.The_Parameter.Last then
929 return String'(1 .. 0 => ' ');
931 return Argument
(Parser
, Parser
.The_Parameter
.Arg_Num
)
932 (Parser
.The_Parameter
.First
.. Parser
.The_Parameter
.Last
);
941 (Parser
: Opt_Parser
:= Command_Line_Parser
) return Character
944 return Parser
.The_Separator
;
951 procedure Set_Parameter
952 (Variable
: out Parameter_Type
;
956 Extra
: Character := ASCII
.NUL
)
959 Variable
.Arg_Num
:= Arg_Num
;
960 Variable
.First
:= First
;
961 Variable
.Last
:= Last
;
962 Variable
.Extra
:= Extra
;
965 ---------------------
966 -- Start_Expansion --
967 ---------------------
969 procedure Start_Expansion
970 (Iterator
: out Expansion_Iterator
;
972 Directory
: String := "";
973 Basic_Regexp
: Boolean := True)
975 Directory_Separator
: Character;
976 pragma Import
(C
, Directory_Separator
, "__gnat_dir_separator");
978 First
: Positive := Pattern
'First;
979 Pat
: String := Pattern
;
982 Canonical_Case_File_Name
(Pat
);
983 Iterator
.Current_Depth
:= 1;
985 -- If Directory is unspecified, use the current directory ("./" or ".\")
987 if Directory
= "" then
988 Iterator
.Dir_Name
(1 .. 2) := "." & Directory_Separator
;
992 Iterator
.Dir_Name
(1 .. Directory
'Length) := Directory
;
993 Iterator
.Start
:= Directory
'Length + 1;
994 Canonical_Case_File_Name
(Iterator
.Dir_Name
(1 .. Directory
'Length));
996 -- Make sure that the last character is a directory separator
998 if Directory
(Directory
'Last) /= Directory_Separator
then
999 Iterator
.Dir_Name
(Iterator
.Start
) := Directory_Separator
;
1000 Iterator
.Start
:= Iterator
.Start
+ 1;
1004 Iterator
.Levels
(1).Name_Last
:= Iterator
.Start
- 1;
1006 -- Open the initial Directory, at depth 1
1008 GNAT
.Directory_Operations
.Open
1009 (Iterator
.Levels
(1).Dir
, Iterator
.Dir_Name
(1 .. Iterator
.Start
- 1));
1011 -- If in the current directory and the pattern starts with "./" or ".\",
1012 -- drop the "./" or ".\" from the pattern.
1014 if Directory
= "" and then Pat
'Length > 2
1015 and then Pat
(Pat
'First) = '.'
1016 and then Pat
(Pat
'First + 1) = Directory_Separator
1018 First
:= Pat
'First + 2;
1022 GNAT
.Regexp
.Compile
(Pat
(First
.. Pat
'Last), Basic_Regexp
, True);
1024 Iterator
.Maximum_Depth
:= 1;
1026 -- Maximum_Depth is equal to 1 plus the number of directory separators
1029 for Index
in First
.. Pat
'Last loop
1030 if Pat
(Index
) = Directory_Separator
then
1031 Iterator
.Maximum_Depth
:= Iterator
.Maximum_Depth
+ 1;
1032 exit when Iterator
.Maximum_Depth
= Max_Depth
;
1035 end Start_Expansion
;
1041 procedure Free
(Parser
: in out Opt_Parser
) is
1042 procedure Unchecked_Free
is new Ada
.Unchecked_Deallocation
1043 (Opt_Parser_Data
, Opt_Parser
);
1046 and then Parser
/= Command_Line_Parser
1048 Free
(Parser
.Arguments
);
1049 Unchecked_Free
(Parser
);
1053 ------------------------
1054 -- Args_From_Expanded --
1055 ------------------------
1057 function Args_From_Expanded
(Args
: Boolean_Chars
) return String is
1058 Result
: String (1 .. Args
'Length);
1059 Index
: Natural := Result
'First;
1062 for A
in Args
'Range loop
1064 Result
(Index
) := A
;
1069 return Result
(1 .. Index
- 1);
1070 end Args_From_Expanded
;
1076 procedure Define_Alias
1077 (Config
: in out Command_Line_Configuration
;
1082 if Config
= null then
1083 Config
:= new Command_Line_Configuration_Record
;
1086 Append
(Config
.Aliases
, new String'(Switch));
1087 Append (Config.Expansions, new String'(Expanded
));
1094 procedure Define_Prefix
1095 (Config
: in out Command_Line_Configuration
;
1099 if Config
= null then
1100 Config
:= new Command_Line_Configuration_Record
;
1103 Append
(Config
.Prefixes
, new String'(Prefix));
1106 -----------------------
1107 -- Set_Configuration --
1108 -----------------------
1110 procedure Set_Configuration
1111 (Cmd : in out Command_Line;
1112 Config : Command_Line_Configuration)
1115 Cmd.Config := Config;
1116 end Set_Configuration;
1118 -----------------------
1119 -- Get_Configuration --
1120 -----------------------
1122 function Get_Configuration
1123 (Cmd : Command_Line) return Command_Line_Configuration is
1126 end Get_Configuration;
1128 ----------------------
1129 -- Set_Command_Line --
1130 ----------------------
1132 procedure Set_Command_Line
1133 (Cmd : in out Command_Line;
1135 Getopt_Description : String := "";
1136 Switch_Char : Character := '-')
1138 Tmp : Argument_List_Access;
1139 Parser : Opt_Parser;
1143 Free (Cmd.Expanded);
1146 if Switches /= "" then
1147 Tmp := Argument_String_To_List (Switches);
1148 Initialize_Option_Scan (Parser, Tmp, Switch_Char);
1152 S := Getopt (Switches => "* " & Getopt_Description,
1153 Concatenate => False,
1155 exit when S = ASCII.NUL;
1158 Add_Switch (Cmd, Full_Switch (Parser), Parameter (Parser),
1159 Separator (Parser));
1162 (Cmd, Switch_Char & Full_Switch (Parser),
1163 Parameter (Parser), Separator (Parser));
1167 when Invalid_Parameter =>
1168 -- Add it with no parameter, if that's the way the user
1170 Add_Switch (Cmd, Switch_Char & Full_Switch (Parser));
1176 end Set_Command_Line;
1185 Substring : String) return Boolean is
1187 return Index + Substring'Length - 1 <= Type_Str'Last
1188 and then Type_Str (Index .. Index + Substring'Length - 1) = Substring;
1191 ----------------------------
1192 -- For_Each_Simple_Switch --
1193 ----------------------------
1195 procedure For_Each_Simple_Switch
1196 (Cmd : Command_Line;
1200 -- Are we adding a switch that can in fact be expanded through aliases ?
1201 -- If yes, we add separately each of its expansion.
1203 -- This takes care of expansions like "-T" -> "-gnatwrs", where the
1204 -- alias and its expansion do not have the same prefix. Given the order
1205 -- in which we do things here, the expansion of the alias will itself
1206 -- be checked for a common prefix and further split into simple switches
1208 if Cmd.Config /= null
1209 and then Cmd.Config.Aliases /= null
1211 for A in Cmd.Config.Aliases'Range loop
1212 if Cmd.Config.Aliases (A).all = Switch then
1213 For_Each_Simple_Switch
1214 (Cmd, Cmd.Config.Expansions (A).all);
1220 -- Are we adding a switch grouping several switches ? If yes, add each
1221 -- of the simple switches instead.
1223 if Cmd.Config /= null
1224 and then Cmd.Config.Prefixes /= null
1226 for P in Cmd.Config.Prefixes'Range loop
1227 if Switch'Length > Cmd.Config.Prefixes (P)'Length + 1
1229 (Switch, Switch'First, Cmd.Config.Prefixes (P).all)
1231 -- Alias expansion will be done recursively
1233 for S in Switch'First + Cmd.Config.Prefixes (P)'Length
1236 For_Each_Simple_Switch
1237 (Cmd, Cmd.Config.Prefixes (P).all & Switch (S));
1245 end For_Each_Simple_Switch;
1251 procedure Add_Switch
1252 (Cmd : in out Command_Line;
1254 Parameter : String := "";
1255 Separator : Character := ' ')
1257 procedure Add_Simple_Switch (Simple : String);
1258 -- Add a new switch that has had all its aliases expanded, and switches
1259 -- ungrouped. We know there is no more aliases in Switches
1261 -----------------------
1262 -- Add_Simple_Switch --
1263 -----------------------
1265 procedure Add_Simple_Switch (Simple : String) is
1267 if Cmd.Expanded = null then
1268 Cmd.Expanded := new Argument_List'(1 .. 1 => new String'(Simple));
1269 if Parameter = "" then
1270 Cmd.Params := new Argument_List'(1 .. 1 => null);
1272 Cmd
.Params
:= new Argument_List
'
1273 (1 .. 1 => new String'(Separator
& Parameter
));
1277 -- Do we already have this switch ?
1279 for C
in Cmd
.Expanded
'Range loop
1280 if Cmd
.Expanded
(C
).all = Simple
1282 ((Cmd
.Params
(C
) = null and then Parameter
= "")
1284 (Cmd
.Params
(C
) /= null
1285 and then Cmd
.Params
(C
).all = Separator
& Parameter
))
1291 Append
(Cmd
.Expanded
, new String'(Simple));
1293 if Parameter = "" then
1294 Append (Cmd.Params, null);
1296 Append (Cmd.Params, new String'(Separator
& Parameter
));
1299 end Add_Simple_Switch
;
1301 procedure Add_Simple_Switches
is
1302 new For_Each_Simple_Switch
(Add_Simple_Switch
);
1304 -- Start of processing for Add_Switch
1307 Add_Simple_Switches
(Cmd
, Switch
);
1308 Free
(Cmd
.Coalesce
);
1315 procedure Remove
(Line
: in out Argument_List_Access
; Index
: Integer) is
1316 Tmp
: Argument_List_Access
:= Line
;
1319 Line
:= new Argument_List
(Tmp
'First .. Tmp
'Last - 1);
1321 if Index
/= Tmp
'First then
1322 Line
(Tmp
'First .. Index
- 1) := Tmp
(Tmp
'First .. Index
- 1);
1327 if Index
/= Tmp
'Last then
1328 Line
(Index
.. Tmp
'Last - 1) := Tmp
(Index
+ 1 .. Tmp
'Last);
1331 Unchecked_Free
(Tmp
);
1339 (Line
: in out Argument_List_Access
;
1340 Str
: String_Access
)
1342 Tmp
: Argument_List_Access
:= Line
;
1345 Line
:= new Argument_List
(Tmp
'First .. Tmp
'Last + 1);
1346 Line
(Tmp
'Range) := Tmp
.all;
1347 Unchecked_Free
(Tmp
);
1349 Line
:= new Argument_List
(1 .. 1);
1352 Line
(Line
'Last) := Str
;
1359 procedure Remove_Switch
1360 (Cmd
: in out Command_Line
;
1362 Remove_All
: Boolean := False)
1364 procedure Remove_Simple_Switch
(Simple
: String);
1365 -- Removes a simple switch, with no aliasing or grouping
1367 --------------------------
1368 -- Remove_Simple_Switch --
1369 --------------------------
1371 procedure Remove_Simple_Switch
(Simple
: String) is
1375 if Cmd
.Expanded
/= null then
1376 C
:= Cmd
.Expanded
'First;
1377 while C
<= Cmd
.Expanded
'Last loop
1378 if Cmd
.Expanded
(C
).all = Simple
then
1379 Remove
(Cmd
.Expanded
, C
);
1380 Remove
(Cmd
.Params
, C
);
1382 if not Remove_All
then
1391 end Remove_Simple_Switch
;
1393 procedure Remove_Simple_Switches
is
1394 new For_Each_Simple_Switch
(Remove_Simple_Switch
);
1396 -- Start of processing for Remove_Switch
1399 Remove_Simple_Switches
(Cmd
, Switch
);
1400 Free
(Cmd
.Coalesce
);
1407 procedure Remove_Switch
1408 (Cmd
: in out Command_Line
;
1412 procedure Remove_Simple_Switch
(Simple
: String);
1413 -- Removes a simple switch, with no aliasing or grouping
1415 --------------------------
1416 -- Remove_Simple_Switch --
1417 --------------------------
1419 procedure Remove_Simple_Switch
(Simple
: String) is
1423 if Cmd
.Expanded
/= null then
1424 C
:= Cmd
.Expanded
'First;
1425 while C
<= Cmd
.Expanded
'Last loop
1426 if Cmd
.Expanded
(C
).all = Simple
1428 ((Cmd
.Params
(C
) = null and then Parameter
= "")
1430 (Cmd
.Params
(C
) /= null
1433 -- Ignore the separator stored in Parameter
1435 Cmd
.Params
(C
) (Cmd
.Params
(C
)'First + 1
1436 .. Cmd
.Params
(C
)'Last) =
1439 Remove
(Cmd
.Expanded
, C
);
1440 Remove
(Cmd
.Params
, C
);
1442 -- The switch is necessarily unique by construction of
1452 end Remove_Simple_Switch
;
1454 procedure Remove_Simple_Switches
is
1455 new For_Each_Simple_Switch
(Remove_Simple_Switch
);
1457 -- Start of processing for Remove_Switch
1460 Remove_Simple_Switches
(Cmd
, Switch
);
1461 Free
(Cmd
.Coalesce
);
1464 --------------------
1465 -- Group_Switches --
1466 --------------------
1468 procedure Group_Switches
1469 (Cmd
: Command_Line
;
1470 Result
: Argument_List_Access
;
1471 Params
: Argument_List_Access
)
1473 type Boolean_Array
is array (Result
'Range) of Boolean;
1475 Matched
: Boolean_Array
;
1478 From_Args
: Boolean_Chars
;
1481 if Cmd
.Config
= null
1482 or else Cmd
.Config
.Prefixes
= null
1487 for P
in Cmd
.Config
.Prefixes
'Range loop
1488 Matched
:= (others => False);
1491 for C
in Result
'Range loop
1492 if Result
(C
) /= null
1493 and then Params
(C
) = null -- ignored if has a parameter
1495 (Result
(C
).all, Result
(C
)'First, Cmd
.Config
.Prefixes
(P
).all)
1497 Matched
(C
) := True;
1503 From_Args
:= (others => False);
1506 for M
in Matched
'Range loop
1512 for A
in Result
(M
)'First + Cmd
.Config
.Prefixes
(P
)'Length
1515 From_Args
(Result
(M
)(A
)) := True;
1521 Result
(First
) := new String'
1522 (Cmd.Config.Prefixes (P).all & Args_From_Expanded (From_Args));
1527 --------------------
1528 -- Alias_Switches --
1529 --------------------
1531 procedure Alias_Switches
1532 (Cmd : Command_Line;
1533 Result : Argument_List_Access;
1534 Params : Argument_List_Access)
1539 procedure Check_Cb (Switch : String);
1540 -- Comment required ???
1542 procedure Remove_Cb (Switch : String);
1543 -- Comment required ???
1549 procedure Check_Cb (Switch : String) is
1552 for E in Result'Range loop
1553 if Result (E) /= null
1554 and then Params (E) = null -- Ignore if has a param
1555 and then Result (E).all = Switch
1569 procedure Remove_Cb (Switch : String) is
1571 for E in Result'Range loop
1572 if Result (E) /= null and then Result (E).all = Switch then
1582 procedure Check_All is new For_Each_Simple_Switch (Check_Cb);
1583 procedure Remove_All is new For_Each_Simple_Switch (Remove_Cb);
1585 -- Start of processing for Alias_Switches
1588 if Cmd.Config = null
1589 or else Cmd.Config.Aliases = null
1594 for A in Cmd.Config.Aliases'Range loop
1596 -- Compute the various simple switches that make up the alias. We
1597 -- split the expansion into as many simple switches as possible, and
1598 -- then check whether the expanded command line has all of them.
1601 Check_All (Cmd, Cmd.Config.Expansions (A).all);
1604 First := Integer'Last;
1605 Remove_All (Cmd, Cmd.Config.Expansions (A).all);
1606 Result (First) := new String'(Cmd
.Config
.Aliases
(A
).all);
1616 (Cmd
: in out Command_Line
;
1617 Iter
: in out Command_Line_Iterator
;
1621 if Cmd
.Expanded
= null then
1626 -- Coalesce the switches as much as possible
1629 and then Cmd
.Coalesce
= null
1631 Cmd
.Coalesce
:= new Argument_List
(Cmd
.Expanded
'Range);
1632 for E
in Cmd
.Expanded
'Range loop
1633 Cmd
.Coalesce
(E
) := new String'(Cmd.Expanded (E).all);
1636 -- Not a clone, since we will not modify the parameters anyway
1638 Cmd.Coalesce_Params := Cmd.Params;
1639 Alias_Switches (Cmd, Cmd.Coalesce, Cmd.Params);
1640 Group_Switches (Cmd, Cmd.Coalesce, Cmd.Params);
1644 Iter.List := Cmd.Expanded;
1645 Iter.Params := Cmd.Params;
1647 Iter.List := Cmd.Coalesce;
1648 Iter.Params := Cmd.Coalesce_Params;
1651 if Iter.List = null then
1652 Iter.Current := Integer'Last;
1654 Iter.Current := Iter.List'First;
1655 while Iter.Current <= Iter.List'Last
1656 and then Iter.List (Iter.Current) = null
1658 Iter.Current := Iter.Current + 1;
1663 --------------------
1664 -- Current_Switch --
1665 --------------------
1667 function Current_Switch (Iter : Command_Line_Iterator) return String is
1669 return Iter.List (Iter.Current).all;
1672 -----------------------
1673 -- Current_Separator --
1674 -----------------------
1676 function Current_Separator (Iter : Command_Line_Iterator) return String is
1678 if Iter.Params = null
1679 or else Iter.Current > Iter.Params'Last
1680 or else Iter.Params (Iter.Current) = null
1686 Sep : constant Character :=
1687 Iter.Params (Iter.Current) (Iter.Params (Iter.Current)'First);
1689 if Sep = ASCII.NUL then
1696 end Current_Separator;
1698 -----------------------
1699 -- Current_Parameter --
1700 -----------------------
1702 function Current_Parameter (Iter : Command_Line_Iterator) return String is
1704 if Iter.Params = null
1705 or else Iter.Current > Iter.Params'Last
1706 or else Iter.Params (Iter.Current) = null
1712 P : constant String := Iter.Params (Iter.Current).all;
1717 return P (P'First + 1 .. P'Last);
1720 end Current_Parameter;
1726 function Has_More (Iter : Command_Line_Iterator) return Boolean is
1728 return Iter.List /= null and then Iter.Current <= Iter.List'Last;
1735 procedure Next (Iter : in out Command_Line_Iterator) is
1737 Iter.Current := Iter.Current + 1;
1738 while Iter.Current <= Iter.List'Last
1739 and then Iter.List (Iter.Current) = null
1741 Iter.Current := Iter.Current + 1;
1749 procedure Free (Config : in out Command_Line_Configuration) is
1751 if Config /= null then
1752 Free (Config.Aliases);
1753 Free (Config.Expansions);
1754 Free (Config.Prefixes);
1755 Unchecked_Free (Config);
1763 procedure Free (Cmd : in out Command_Line) is
1765 Free (Cmd.Expanded);
1766 Free (Cmd.Coalesce);
1770 end GNAT.Command_Line;