* doc/install.texi (Prerequisites): New section documenting
[official-gcc.git] / gcc / ada / g-comlin.ads
blob1e421e4cd3ec9de8bd0952d6e45bef4203e6f8fc
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . C O M M A N D _ L I N E --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1999-2002 Ada Core Technologies, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
30 -- --
31 ------------------------------------------------------------------------------
33 -- High level package for command line parsing
35 -- This package provides an interface to Ada.Command_Line, to do the
36 -- parsing of command line arguments. Here is a small usage example:
38 -- begin
39 -- loop
40 -- case Getopt ("a b: ad") is -- Accepts '-a', '-ad', or '-b argument'
41 -- when ASCII.NUL => exit;
43 -- when 'a' =>
44 -- if Full_Switch = "a" then
45 -- Put_Line ("Got a");
46 -- else
47 -- Put_Line ("Got ad");
48 -- end if;
50 -- when 'b' =>
51 -- Put_Line ("Got b + " & Parameter);
53 -- when others =>
54 -- raise Program_Error; -- cannot occur!
55 -- end case;
56 -- end loop;
58 -- loop
59 -- declare
60 -- S : constant String := Get_Argument (Do_Expansion => True);
61 -- begin
62 -- exit when S'Length = 0;
63 -- Put_Line ("Got " & S);
64 -- end;
65 -- end loop;
67 -- exception
68 -- when Invalid_Switch => Put_Line ("Invalid Switch " & Full_Switch);
69 -- when Invalid_Parameter => Put_Line ("No parameter for " & Full_Switch);
70 -- end;
72 -- A more complicated example would involve the use of sections for the
73 -- switches, as for instance in gnatmake. These sections are separated by
74 -- special switches, chosen by the programer. Each section act as a
75 -- command line of its own.
77 -- begin
78 -- Initialize_Option_Scan ('-', False, "largs bargs cargs");
79 -- loop
80 -- -- Same loop as above to get switches and arguments
81 -- end loop;
83 -- Goto_Section ("bargs");
84 -- loop
85 -- -- Same loop as above to get switches and arguments
86 -- -- The supports switches in Get_Opt might be different
87 -- end loop;
89 -- Goto_Section ("cargs");
90 -- loop
91 -- -- Same loop as above to get switches and arguments
92 -- -- The supports switches in Get_Opt might be different
93 -- end loop;
94 -- end;
96 with GNAT.Directory_Operations;
97 with GNAT.Regexp;
99 package GNAT.Command_Line is
101 procedure Initialize_Option_Scan
102 (Switch_Char : Character := '-';
103 Stop_At_First_Non_Switch : Boolean := False;
104 Section_Delimiters : String := "");
105 -- This procedure resets the internal state of the package to prepare
106 -- to rescan the parameters. It need not (but may be) called before the
107 -- first use of Getopt, but it must be called if you want to start
108 -- rescanning the command line parameters from the start. The optional
109 -- parameter Switch_Char can be used to reset the switch character,
110 -- e.g. to '/' for use in DOS-like systems. The optional parameter
111 -- Stop_At_First_Non_Switch indicates if Getopt is to look for switches
112 -- on the whole command line, or if it has to stop as soon as a
113 -- non-switch argument is found.
115 -- Example:
117 -- Arguments: my_application file1 -c
119 -- if Stop_At_First_Non_Switch is False, then -c will be considered
120 -- as a switch (returned by getopt), otherwise it will be considered
121 -- as a normal argument (returned by Get_Argument).
123 -- if SECTION_DELIMITERS is set, then every following subprogram
124 -- (Getopt and Get_Argument) will only operate within a section, which
125 -- is delimited by any of these delimiters or the end of the command line.
127 -- Example:
128 -- Initialize_Option_Scan ("largs bargs cargs");
130 -- Arguments on command line : my_application -c -bargs -d -e -largs -f
131 -- This line is made of three section, the first one is the default one
132 -- and includes only the '-c' switch, the second one is between -bargs
133 -- and -largs and includes '-d -e' and the last one includes '-f'
135 procedure Goto_Section (Name : String := "");
136 -- Change the current section. The next Getopt of Get_Argument will
137 -- start looking at the beginning of the section. An empty name ("")
138 -- refers to the first section between the program name and the first
139 -- section delimiter.
140 -- If the section does not exist, then Invalid_Section is raised.
142 function Full_Switch return String;
143 -- Returns the full name of the last switch found (Getopt only returns
144 -- the first character)
146 function Getopt (Switches : String) return Character;
147 -- This function moves to the next switch on the command line (defined
148 -- as a switch character followed by a character within Switches,
149 -- casing being significant). The result returned is the first
150 -- character of the particular switch located. If there are no more
151 -- switches in the current section, returns ASCII.NUL. The switches
152 -- need not be separated by spaces (they can be concatenated if they do
153 -- not require an argument, e.g. -ab is the same as two separate
154 -- arguments -a -b).
156 -- Switches is a string of all the possible switches, separated by a
157 -- space. A switch can be followed by one of the following characters :
159 -- ':' The switch requires a parameter. There can optionally be a space
160 -- on the command line between the switch and its parameter
161 -- '=' The switch requires a parameter. There can either be a '=' or a
162 -- space on the command line between the switch and its parameter
163 -- '!' The switch requires a parameter, but there can be no space on the
164 -- command line between the switch and its parameter
165 -- '?' The switch may have an optional parameter. There can no space
166 -- between the switch and its argument
167 -- ex/ if Switches has the following value : "a? b"
168 -- The command line can be :
169 -- -afoo : -a switch with 'foo' parameter
170 -- -a foo : -a switch and another element on the
171 -- command line 'foo', returned by Get_Argument
173 -- Example: if Switches is "-a: -aO:", you can have the following
174 -- command lines :
175 -- -aarg : 'a' switch with 'arg' parameter
176 -- -a arg : 'a' switch with 'arg' parameter
177 -- -aOarg : 'aO' switch with 'arg' parameter
178 -- -aO arg : 'aO' switch with 'arg' parameter
180 -- Example:
182 -- Getopt ("a b: ac ad?")
184 -- accept either 'a' or 'ac' with no argument,
185 -- accept 'b' with a required argument
186 -- accept 'ad' with an optional argument
188 -- If the first item in switches is '*', then Getopt will catch
189 -- every element on the command line that was not caught by any other
190 -- switch. The character returned by GetOpt is '*'
192 -- Example
193 -- Getopt ("* a b")
194 -- If the command line is '-a -c toto.o -b', GetOpt will return
195 -- successively 'a', '*', '*' and 'b'. When '*' is returnd,
196 -- Full_Switch returns the corresponding item on the command line.
199 -- When Getopt encounters an invalid switch, it raises the exception
200 -- Invalid_Switch and sets Full_Switch to return the invalid switch.
201 -- When Getopt can not find the parameter associated with a switch, it
202 -- raises Invalid_Parameter, and sets Full_Switch to return the invalid
203 -- switch character.
205 -- Note: in case of ambiguity, e.g. switches a ab abc, then the longest
206 -- matching switch is returned.
208 -- Arbitrary characters are allowed for switches, although it is
209 -- strongly recommanded to use only letters and digits for portability
210 -- reasons.
212 function Get_Argument (Do_Expansion : Boolean := False) return String;
213 -- Returns the next element in the command line which is not a switch.
214 -- This function should not be called before Getopt has returned
215 -- ASCII.NUL.
217 -- If Expansion is True, then the parameter on the command
218 -- line will considered as filename with wild cards, and will be
219 -- expanded. The matching file names will be returned one at a time.
220 -- When there are no more arguments on the command line, this function
221 -- returns an empty string. This is useful in non-Unix systems for
222 -- obtaining normal expansion of wild card references.
224 function Parameter return String;
225 -- Returns parameter associated with the last switch returned by Getopt.
226 -- If no parameter was associated with the last switch, or no previous
227 -- call has been made to Get_Argument, raises Invalid_Parameter.
228 -- If the last switch was associated with an optional argument and this
229 -- argument was not found on the command line, Parameter returns an empty
230 -- string
232 type Expansion_Iterator is limited private;
233 -- Type used during expansion of file names
235 procedure Start_Expansion
236 (Iterator : out Expansion_Iterator;
237 Pattern : String;
238 Directory : String := "";
239 Basic_Regexp : Boolean := True);
240 -- Initialize a wild card expansion. The next calls to Expansion will
241 -- return the next file name in Directory which match Pattern (Pattern
242 -- is a regular expression, using only the Unix shell and DOS syntax if
243 -- Basic_Regexp is True). When Directory is an empty string, the current
244 -- directory is searched.
246 -- Pattern may contains directory separators (as in "src/*/*.ada").
247 -- Subdirectories of Directory will also be searched, up to one
248 -- hundred levels deep.
250 -- When Start_Expansion has been called, function Expansion should be
251 -- called repetitively until it returns an empty string, before
252 -- Start_Expansion can be called again with the same Expansion_Iterator
253 -- variable.
255 function Expansion (Iterator : Expansion_Iterator) return String;
256 -- Return the next file in the directory matching the parameters given
257 -- to Start_Expansion and updates Iterator to point to the next entry.
258 -- Returns an empty string when there are no more files in the directory
259 -- and its subdirectories.
261 -- If Expansion is called again after an empty string has been returned,
262 -- then the exception GNAT.Directory_Operations.Directory_Error is raised.
264 Invalid_Section : exception;
265 -- Raised when an invalid section is selected by Goto_Section
267 Invalid_Switch : exception;
268 -- Raised when an invalid switch is detected in the command line
270 Invalid_Parameter : exception;
271 -- Raised when a parameter is missing, or an attempt is made to obtain
272 -- a parameter for a switch that does not allow a parameter
274 private
276 Max_Depth : constant := 100;
277 -- Maximum depth of subdirectories
279 Max_Path_Length : constant := 1024;
280 -- Maximum length of relative path
282 type Depth is range 1 .. Max_Depth;
284 type Level is record
285 Name_Last : Natural := 0;
286 Dir : GNAT.Directory_Operations.Dir_Type;
287 end record;
289 type Level_Array is array (Depth) of Level;
291 type Expansion_Iterator is limited record
292 Start : Positive := 1;
293 -- Position of the first character of the relative path to check
294 -- against the pattern.
296 Dir_Name : String (1 .. Max_Path_Length);
298 Current_Depth : Depth := 1;
300 Levels : Level_Array;
302 Regexp : GNAT.Regexp.Regexp;
303 -- Regular expression built with the pattern
305 Maximum_Depth : Depth := 1;
306 -- The maximum depth of directories, reflecting the number of
307 -- directory separators in the pattern.
309 end record;
311 end GNAT.Command_Line;