2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / g-comlin.ads
blobd5ff4e990052311b3e72004d80c370e9b05d9e27
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-2003 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 was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- High level package for command line parsing
36 -- This package provides an interface to Ada.Command_Line, to do the
37 -- parsing of command line arguments. Here is a small usage example:
39 -- begin
40 -- loop
41 -- case Getopt ("a b: ad") is -- Accepts '-a', '-ad', or '-b argument'
42 -- when ASCII.NUL => exit;
44 -- when 'a' =>
45 -- if Full_Switch = "a" then
46 -- Put_Line ("Got a");
47 -- else
48 -- Put_Line ("Got ad");
49 -- end if;
51 -- when 'b' =>
52 -- Put_Line ("Got b + " & Parameter);
54 -- when others =>
55 -- raise Program_Error; -- cannot occur!
56 -- end case;
57 -- end loop;
59 -- loop
60 -- declare
61 -- S : constant String := Get_Argument (Do_Expansion => True);
62 -- begin
63 -- exit when S'Length = 0;
64 -- Put_Line ("Got " & S);
65 -- end;
66 -- end loop;
68 -- exception
69 -- when Invalid_Switch => Put_Line ("Invalid Switch " & Full_Switch);
70 -- when Invalid_Parameter => Put_Line ("No parameter for " & Full_Switch);
71 -- end;
73 -- A more complicated example would involve the use of sections for the
74 -- switches, as for instance in gnatmake. These sections are separated by
75 -- special switches, chosen by the programer. Each section act as a
76 -- command line of its own.
78 -- begin
79 -- Initialize_Option_Scan ('-', False, "largs bargs cargs");
80 -- loop
81 -- -- Same loop as above to get switches and arguments
82 -- end loop;
84 -- Goto_Section ("bargs");
85 -- loop
86 -- -- Same loop as above to get switches and arguments
87 -- -- The supports switches in Get_Opt might be different
88 -- end loop;
90 -- Goto_Section ("cargs");
91 -- loop
92 -- -- Same loop as above to get switches and arguments
93 -- -- The supports switches in Get_Opt might be different
94 -- end loop;
95 -- end;
97 with GNAT.Directory_Operations;
98 with GNAT.Regexp;
100 package GNAT.Command_Line is
102 procedure Initialize_Option_Scan
103 (Switch_Char : Character := '-';
104 Stop_At_First_Non_Switch : Boolean := False;
105 Section_Delimiters : String := "");
106 -- This procedure resets the internal state of the package to prepare
107 -- to rescan the parameters. It need not (but may be) called before the
108 -- first use of Getopt, but it must be called if you want to start
109 -- rescanning the command line parameters from the start. The optional
110 -- parameter Switch_Char can be used to reset the switch character,
111 -- e.g. to '/' for use in DOS-like systems. The optional parameter
112 -- Stop_At_First_Non_Switch indicates if Getopt is to look for switches
113 -- on the whole command line, or if it has to stop as soon as a
114 -- non-switch argument is found.
116 -- Example:
118 -- Arguments: my_application file1 -c
120 -- if Stop_At_First_Non_Switch is False, then -c will be considered
121 -- as a switch (returned by getopt), otherwise it will be considered
122 -- as a normal argument (returned by Get_Argument).
124 -- if SECTION_DELIMITERS is set, then every following subprogram
125 -- (Getopt and Get_Argument) will only operate within a section, which
126 -- is delimited by any of these delimiters or the end of the command line.
128 -- Example:
129 -- Initialize_Option_Scan ("largs bargs cargs");
131 -- Arguments on command line : my_application -c -bargs -d -e -largs -f
132 -- This line is made of three section, the first one is the default one
133 -- and includes only the '-c' switch, the second one is between -bargs
134 -- and -largs and includes '-d -e' and the last one includes '-f'
136 procedure Goto_Section (Name : String := "");
137 -- Change the current section. The next Getopt of Get_Argument will
138 -- start looking at the beginning of the section. An empty name ("")
139 -- refers to the first section between the program name and the first
140 -- section delimiter.
141 -- If the section does not exist, then Invalid_Section is raised.
143 function Full_Switch return String;
144 -- Returns the full name of the last switch found (Getopt only returns
145 -- the first character)
147 function Getopt
148 (Switches : String;
149 Concatenate : Boolean := True) return Character;
150 -- This function moves to the next switch on the command line (defined
151 -- as a switch character followed by a character within Switches,
152 -- casing being significant). The result returned is the first
153 -- character of the particular switch located. If there are no more
154 -- switches in the current section, returns ASCII.NUL. If Concatenate is
155 -- True (by default), the switches need not be separated by spaces (they
156 -- can be concatenated if they do not require an argument, e.g. -ab is the
157 -- same as two separate arguments -a -b).
159 -- Switches is a string of all the possible switches, separated by a
160 -- space. A switch can be followed by one of the following characters :
162 -- ':' The switch requires a parameter. There can optionally be a space
163 -- on the command line between the switch and its parameter
164 -- '=' The switch requires a parameter. There can either be a '=' or a
165 -- space on the command line between the switch and its parameter
166 -- '!' The switch requires a parameter, but there can be no space on the
167 -- command line between the switch and its parameter
168 -- '?' The switch may have an optional parameter. There can be no space
169 -- between the switch and its argument
170 -- ex/ if Switches has the following value : "a? b"
171 -- The command line can be :
172 -- -afoo : -a switch with 'foo' parameter
173 -- -a foo : -a switch and another element on the
174 -- command line 'foo', returned by Get_Argument
176 -- Example: if Switches is "-a: -aO:", you can have the following
177 -- command lines :
178 -- -aarg : 'a' switch with 'arg' parameter
179 -- -a arg : 'a' switch with 'arg' parameter
180 -- -aOarg : 'aO' switch with 'arg' parameter
181 -- -aO arg : 'aO' switch with 'arg' parameter
183 -- Example:
185 -- Getopt ("a b: ac ad?")
187 -- accept either 'a' or 'ac' with no argument,
188 -- accept 'b' with a required argument
189 -- accept 'ad' with an optional argument
191 -- If the first item in switches is '*', then Getopt will catch
192 -- every element on the command line that was not caught by any other
193 -- switch. The character returned by GetOpt is '*'
195 -- Example
196 -- Getopt ("* a b")
197 -- If the command line is '-a -c toto.o -b', GetOpt will return
198 -- successively 'a', '*', '*' and 'b'. When '*' is returnd,
199 -- Full_Switch returns the corresponding item on the command line.
202 -- When Getopt encounters an invalid switch, it raises the exception
203 -- Invalid_Switch and sets Full_Switch to return the invalid switch.
204 -- When Getopt can not find the parameter associated with a switch, it
205 -- raises Invalid_Parameter, and sets Full_Switch to return the invalid
206 -- switch character.
208 -- Note: in case of ambiguity, e.g. switches a ab abc, then the longest
209 -- matching switch is returned.
211 -- Arbitrary characters are allowed for switches, although it is
212 -- strongly recommanded to use only letters and digits for portability
213 -- reasons.
215 -- When Concatenate is False, individual switches need to be separated by
216 -- spaces.
218 -- Example
219 -- Getopt ("a b", Concatenate => False)
220 -- If the command line is '-ab', exception Invalid_Switch will be
221 -- raised and Full_Switch will return "ab".
223 function Get_Argument (Do_Expansion : Boolean := False) return String;
224 -- Returns the next element in the command line which is not a switch.
225 -- This function should not be called before Getopt has returned
226 -- ASCII.NUL.
228 -- If Expansion is True, then the parameter on the command
229 -- line will considered as filename with wild cards, and will be
230 -- expanded. The matching file names will be returned one at a time.
231 -- When there are no more arguments on the command line, this function
232 -- returns an empty string. This is useful in non-Unix systems for
233 -- obtaining normal expansion of wild card references.
235 function Parameter return String;
236 -- Returns parameter associated with the last switch returned by Getopt.
237 -- If no parameter was associated with the last switch, or no previous
238 -- call has been made to Get_Argument, raises Invalid_Parameter.
239 -- If the last switch was associated with an optional argument and this
240 -- argument was not found on the command line, Parameter returns an empty
241 -- string
243 type Expansion_Iterator is limited private;
244 -- Type used during expansion of file names
246 procedure Start_Expansion
247 (Iterator : out Expansion_Iterator;
248 Pattern : String;
249 Directory : String := "";
250 Basic_Regexp : Boolean := True);
251 -- Initialize a wild card expansion. The next calls to Expansion will
252 -- return the next file name in Directory which match Pattern (Pattern
253 -- is a regular expression, using only the Unix shell and DOS syntax if
254 -- Basic_Regexp is True). When Directory is an empty string, the current
255 -- directory is searched.
257 -- Pattern may contains directory separators (as in "src/*/*.ada").
258 -- Subdirectories of Directory will also be searched, up to one
259 -- hundred levels deep.
261 -- When Start_Expansion has been called, function Expansion should be
262 -- called repetitively until it returns an empty string, before
263 -- Start_Expansion can be called again with the same Expansion_Iterator
264 -- variable.
266 function Expansion (Iterator : Expansion_Iterator) return String;
267 -- Return the next file in the directory matching the parameters given
268 -- to Start_Expansion and updates Iterator to point to the next entry.
269 -- Returns an empty string when there are no more files in the directory
270 -- and its subdirectories.
272 -- If Expansion is called again after an empty string has been returned,
273 -- then the exception GNAT.Directory_Operations.Directory_Error is raised.
275 Invalid_Section : exception;
276 -- Raised when an invalid section is selected by Goto_Section
278 Invalid_Switch : exception;
279 -- Raised when an invalid switch is detected in the command line
281 Invalid_Parameter : exception;
282 -- Raised when a parameter is missing, or an attempt is made to obtain
283 -- a parameter for a switch that does not allow a parameter
285 private
287 Max_Depth : constant := 100;
288 -- Maximum depth of subdirectories
290 Max_Path_Length : constant := 1024;
291 -- Maximum length of relative path
293 type Depth is range 1 .. Max_Depth;
295 type Level is record
296 Name_Last : Natural := 0;
297 Dir : GNAT.Directory_Operations.Dir_Type;
298 end record;
300 type Level_Array is array (Depth) of Level;
302 type Expansion_Iterator is limited record
303 Start : Positive := 1;
304 -- Position of the first character of the relative path to check
305 -- against the pattern.
307 Dir_Name : String (1 .. Max_Path_Length);
309 Current_Depth : Depth := 1;
311 Levels : Level_Array;
313 Regexp : GNAT.Regexp.Regexp;
314 -- Regular expression built with the pattern
316 Maximum_Depth : Depth := 1;
317 -- The maximum depth of directories, reflecting the number of
318 -- directory separators in the pattern.
320 end record;
322 end GNAT.Command_Line;